This is my first question here on Pro Webmaster... I'm really glad to be here!

I believe this question will have a simple answer but I'm starting with htaccess and I want to learn something about this file and its rules.

I have a long and ugly affiliate link but I want to create a 301 redirect to this URL, without typing it. My website is http://www.matthewlabs.com/ and I want to use a link like http://www.matthewlabs.com/wishonlist/appstore which automatically redirects to my affiliate link.

I tried to write this in my htaccess file:

Redirect 301 /wishonlist/appstore http://myAffiliateLinkHere

but it doesn't work. Must the directory /wishonlist/appstore exist to redirect? Because now I receive a 404 error...
And to use this link, can I create a simple link with html a tag?

<a href="http://www.matthewlabs.com/wishonlist/appstore">Click</a>

Thank you so much for your time and I hope you can help me!


Your .htaccess file needs to look something like this:

RewriteEngine on

RewriteRule [URL to redirect from] [URL to redirect to] [options]

RewriteEngine on basically switches the mod_rewrite module on.

Then you need to say what the URL you're expecting people to enter looks like, you can use regular expressions here, in fact, the whole thing IS a regular expression.

URL to redirect to is usually internal, but it should work with a full link.

So in practice, something like:

RewriteEngine on

RewriteRule ^/wishonlist/appstore(/)?$ http://myAffiliateLinkHere [301,L]

The ^ and $ to denote the start and end of the URL, and (/)? means that it can be entered with or without a trailing slash and still work.

Options - The 301 basically says send a 301 header, and L says this is a 'last instruction' - after which no more instructions should be processed.

There's a good wealth of options available, check the documentation - but here's a link to a good cheat sheet I always keep handy: http://www.addedbytes.com/cheat-sheets/mod_rewrite-cheat-sheet/

  • Thank you so much for your answer... I tried it but without success... This is my htaccess file: cl.ly/Chba Now I get an Internal Server Error
    – matteodv
    Dec 17 '11 at 23:29
  • I would try redirecting to a simpler, short URL on the same machine first, see if that works. If so, then try a short, simple URL on another host, and if so, build up your URL till you find whats causing the problem. Two guesses - one, the space between '301' and 'L', or more likely, some character(s) in the long URL that need to be escaped (with a backslash).
    – Codecraft
    Dec 18 '11 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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