Webmasters Stack Exchange is a question and answer site for pro webmasters. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to set up my .htaccess file to take the displayed link and route it to the destination link as below

Displayed Link http://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 Link http://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

share|improve this question

migrated from stackoverflow.com Feb 10 '11 at 19:32

This question came from our site for professional and enthusiast programmers.

    
What would have been useful, in order to better answer this question, is whether the source and destination websites are actually hosted at the same place/directory or whether they are different servers? Also, is there a requirement to use mod_rewrite? Is mod_rewrite already being used in other parts of the script? – w3dk Dec 16 '15 at 0:12

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]
share|improve this answer
    
Thanks, but I don't want to redirect everything from my-webiste.com. Just links that start http://www.my-website.com/click-4559226-10388358 – Anonymous Feb 10 '11 at 19:29
    
See the edit above – Emmanuel Feb 10 '11 at 19:36
    
Even the edit looks far too general... why redirect /click-0-0 and /click-1-1 when the OP has specifically stated they only want to redirect /click-4559226-10388358? There is no need to escape hyphens in a regex that are outside of a character class. There is also no need to manually append the %{QUERY_STRING} onto the substitution if you aren't already specifying a query string on the substitution. The query string is copied by default. – w3dk Dec 16 '15 at 0:24

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.

share|improve this answer

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.

share|improve this answer
    
A mod_alias Redirect requires a slash prefix on the URL-path (origin URL) regardless of whether it is used in .htaccess or the server config. (Removing the directory-prefix is just a mod_rewrite thing.) It's perhaps worth mentioning that you should never mix redirects from both mod_alias and mod_rewrite (risk of conflict), so whilst mod_alias is more efficient, if you are already using mod_rewrite you should probably stick with mod_rewrite. – w3dk Dec 16 '15 at 9:23
1  
Thanks @w3d: I've updated the answer now. The relevant docs are here if anyone's interested. – Stephan Dec 16 '15 at 21:04

On www.my-website.com:

Redirect permanent / http://www.destination-wabsite.com/
share|improve this answer
    
This redirects everything - which is not the requirement. – w3dk Dec 15 '15 at 23:56

Assuming mod_rewrite is enabled on your server, you would want to put something like:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.my-website\.com$ [NC]
RewriteRule ^(.*)$ http://www.destination-website.com/$1 [QSA,L]
</IfModule>

This will redirect any requests to www.my-website.com to www.destination-website.com. The [NC] tells Apache to operate in case insensitive mode and the [QSA,L] tells it to maintain any querystring parameters and that no other rules should be followed. More information can be found on Apache's website.

share|improve this answer
    
This redirects everything - which is not the requirement. The QSA flag is unnecessary here. Any query string on the request will be copied across by default providing there is no query string on the substitution (which there isn't in this case). The <IfModule> container is also unnecessary, since this code is dependent on mod_rewrite being enabled - it is better to fail loudly, rather than silently in this case. – w3dk Dec 16 '15 at 0:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.