Redirect a single URL to the same URL on another domain
I'm trying to set up my .htaccess file to take the displayed link and route it to the destination link as below
Displayed Linkhttp://www.my-website.com/click-4559226-10388358?url=https%3A%2F%2Fdestination-website2.com%2FItem.php%3Fid%3D44350396%26sld%3DA6D7A632-821E-4b78-ACD0-147658B77BD6
Destination Linkhttp://www.destination-website.com/click-4559226-10388358?url=https%3A%2F%2Fdestination-website2.com%2FItem.php%3Fid%3D44350396%26sld%3DA6D7A632-821E-4b78-ACD0-147658B77BD6
Effectively, all that changes is the first URL (http://www.my-website.com
) everything after that is the same.
Is this possible and could someone briefly explain how I would go about it?
* Just to be clear, I don't want to redirect everything from my-website.com
. Just links that start http://www.my-website.com/click-4559226-10388358
Try this in your .htaccess
file:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %HTTP_HOST ^www.olddomain.com$[OR]
RewriteCond %HTTP_HOST ^olddomain.com$
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]
If it's not keeping the query string, try changing the last line of the code above to
RewriteRule / http://www.newdomain.com/?%QUERY_STRING [R=301,L]
edit:
If you only want to limit the rule to www.my-website.com/click-4559226-10388358
, then try changing the last line of the code above to this:
RewriteRule ^click-([0-9]+)-([0-9]+)$ http://www.newdomain.com/click-$1-$2?%QUERY_STRING [R=301,L]
Just links that start
http://www.my-website.com/click-4559226-10388358
To redirect just the links where the URL-path is (exactly) /click-4559226-10388358
to the same URL on another domain, whilst maintaining the query string, then you can do something like the following in the .htaccess file in the document root.
This uses mod_rewrite, and should come near the top of the file if you have other directives:
RewriteEngine On
RewriteCond %HTTP_HOST ^(www.)?my-website.com$ [NC]
RewriteRule ^click-4559226-10388358$ http://www.destination-website.com/$0 [R=302,L]
If the source and destination domains are located on different servers then you can omit the RewriteCond
directive.
Change the 302
(temporary) redirect to 301
(permanent) when you are sure it's working OK. Permanent redirects are cached by the browser and so can cause problems when testing.
If all that's changing is the query string, it should be possible to do this without mod_rewrite
. Using a straightforward Redirect
directive is more efficient, and often significantly faster than regular expression matching with a RedirectRule
. Here's the Redirect
statement, in an .htaccess
file in the root of your site:
Redirect /click-4559226-10388358 http://www.destination-website.com/click-4559226-10388358
As w3d has pointed out, you should test that before changing to a permanent redirect:
Redirect 301 /click-4559226-10388358 http://www.destination-website.com/click-4559226-10388358
Redirect
and RedirectMatch
will pass the query string along to the target url unchanged.
On www.my-website.com:
We hope that this article has helped you resolve the htaccess, redirects, error in your web browsers. Enjoy browsing the internet uninterrupted!
Comments
Post a Comment