0

I have two websites https://abc1.com https://example1.com.

Example1 is an ecommerce site. Let's say in the website a product link is https://example1.com/products/123456.html

Now I'm sharing the product and share link is https://abc1.com/products/123456.html

When user is clicking the link,it should redirect to https://example1.com/products/123456.html.

For a static page manually it is possible for each link.but I want it dynamically. You can understand in an ecommerce site there will be tons of products, constantly updating. So that whatever the product id will be placed with abc.com dynamically it should redirect to example1.com.

I have to redirect via js.like https://abc1.com/probucts/xyz123.html --> to a page where js,text/picture will be -->[redirection] --> https://example1.com/products/xyz123.html

If anybody can help.

Debo
  • 3
  • 1
  • 6

1 Answers1

0

You will need to use .htaccess, you need to create a file named .htaccess in the root of your abc1.com website, with this content:

RewriteEngine on
RewriteRule ^/products/([0-9]+).html$ https://example1.com/products/$1.html [R=301,NC,L]

This redirects every URL like this: https://abc1.com/products/123456.html
to this: https://example1.com/products/123456.html

EDIT:

If you need to redirect via JS, you can just rewrite the url to a single page:

RewriteEngine on
RewriteRule ^/products/([0-9]+).html$ /redirect.html?id=$1 [NC,L]

And inside redirect.html put the JS redirection code by using the id parameter

Xriuk
  • 429
  • 5
  • 16
  • 1
    You are assuming the OP is running Apache. I do not see why that assumption may be justified in any way. –  Feb 18 '19 at 12:06
  • @xriuk, thank you very much. It solved 50% of my problem. But you are redirecting directly but for my problem, I have to redirect via js.like https://abc1.com/probucts/xyz123.html --> to a page where js,text/picture will be -->[redirection] --> https://example1.com/products/xyz123.html – Debo Feb 18 '19 at 12:17
  • @Debo in your js code on https://abc1.com/probucts/xyz123.html try with window.location = 'https://example1.com/products/xyz123.html'; – user889030 Feb 18 '19 at 12:23
  • @user889030, for a static page it's possible. But as I said above it's for an ecommerce site. So static page for every link it is not possible.thats why I need your help guys. – Debo Feb 18 '19 at 12:28
  • can you try something like this , you will have to inject this few line of js in your footer so that it load on all pages [code] let your_new_site_host = ' https://example1.com'; let product_path = document.location.pathname; window.location = your_new_site_host+product_path; [/code] – user889030 Feb 18 '19 at 12:44
  • 1
    @xriuk, thanks again. You explained in simple words , and newcomers like me can understand easily. One more question I have against your answer, if abc1.com's link is like https://abc1.com/share/[*****/****] (like - abc1.com/share/products/xyzabz1 or abc1.com/share/vendors/z@12abcwq) and redirect.html file is in a folder named"check" in the server, how to pick id parameter so that redirect url should be https://example1.com/[*****/****]? Same .htaccess code? – Debo Feb 18 '19 at 13:10
  • If I understood correctly you could have multiple "sections" (/), then you could use something like this: `RewriteRule ^share/(.+)$ /check/redirect.html?id=$1 [NC,L]` the `id` parameter would be everything after "share/", so: "products/xyzabz1", "vendors/z@12abcwq", ... – Xriuk Feb 18 '19 at 13:21
  • Thank you @xriuk. Will implement and will let you know the result. – Debo Feb 18 '19 at 15:26