Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

htaccess variable redirect

1:33 pm on May 15, 2007 (gmt 0)

5+ Year Member


Am a little confused about a htaccess redirect.

I am wondering if it is possible to do this?

Redirect this




I am not trying to rewrite the URL, I have already managed that. I am trying to stop access to these variables in the URL and keep it so that anyone who tries to access those variables in the URL will be redirected the rewritten equivalents.

I can find loads of info on rewrites like this

RewriteRule index/(.*)/(.*)/(.*)/(.*)/$ /index.php?$1=$2&$3=$4

But nothing for redirects.

Do i need to use RewriteCond directive?

Any help is much appreciated.


[edited by: Zacharias at 1:34 pm (utc) on May 15, 2007]

[edited by: jdMorgan at 7:12 pm (utc) on May 15, 2007]
[edit reason] example.com [/edit]

3:12 pm on May 15, 2007 (gmt 0)

5+ Year Member

I am using this abc.com to redirect to www.abc.com with all variable.

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.*) http://www\.example\.com/$1 [L]

might be this can help you and you can do change according to your need.
[other member please correct me if i was wrong.]

[edited by: Discovery at 3:14 pm (utc) on May 15, 2007]

[edited by: jdMorgan at 7:13 pm (utc) on May 15, 2007]
[edit reason] example.com [/edit]

6:06 pm on May 15, 2007 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

1) Publish this HTML link on your Web page(s):
 <a href="http://example.com/films/dvds">Order DVDs!</a> 

2) Internally rewrite that URL, when requested from your server, to your script:

RewriteRule ^films/dvds/?$ /index.php?s=films&c=dvds [L]

3) Catch direct client requests (only) for the dynamic URL and externally redirect them to the SE-friendly, static-looking URL:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?s=films&c=dvds[^\ ]*\ HTTP/
RewriteRule ^index\.php$ http://example.com/films/dvds? [R=301,L]

Note that I have used "/films/dvds" as the URL-path; This is the correct way to reference an extensionless file, rather than a directory index. Change to suit.

I assume that all rules are in example.com/.htaccess.

THE_REQUEST is the entire request header received from the client (e.g. browser), and in the case you are trying to address, looks something like this:

GET /films/dvds?s=films&c=dvds HTTP/1.1

By testing THE_REQUEST using a RewriteCond, you prevent the redirect from being invoked as a result of a previously-rewritten request for "/films/dvds". If this test is omitted, then the two rules would each countermand the other, and the result would be an 'infinite' rewrite-redirect loop. This would continue until either the client or the server reached its maximum redirection limit.

The trailing "?" on the external redirect URL is required to clear the existing query string; It *will not* appear in the redirected URL.

[added] For more information, see the "Rewriting static to dynamic URLs" tutorial in the Apache forum section of the WebmasterWorld library [webmasterworld.com]. [/added]


[edited by: jdMorgan at 6:09 pm (utc) on May 15, 2007]

7:16 pm on May 15, 2007 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

The domain redirection code posted above is not relevant to this problem. It's also incorrect in at least three ways. While I do not want this thread to be diverted off-topic, here is the corrected domain canonicalization code:

RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

3:00 am on May 16, 2007 (gmt 0)

5+ Year Member

Thanks for the replies they really helped

I am trying now to add variables to the url like this:

I should also mention that I am trying to redirect the link generated by a form submission, so I end up with + signs instead of spaces that i would like to replace with a -

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /folder/index\.php\?s=([-A-Za-z+]+)[^\ ]*\ HTTP/
RewriteRule ^index\.php$ [] [R=301,L]

This is not working as expected and is giving me:


insted of


I cannot seem to replace the + with a -

I have seen some posts using several of the same redirects like this

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
rewriterule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)\.html$ [mysiteurl.com...] [R=301,L]
rewriterule ^([^_]*)_([^_]*)_([^_]*)_(.*)\.html$ [mysiteurl.com...] [R=301,L]
rewriterule ^([^_]*)_([^_]*)_(.*)\.html$ [mysiteurl.com...] [R=301,L]
rewriterule ^([^_]*)_(.*)\.html$ [mysiteurl.com...] [R=301,L]</code>

is this the solution to my problems?

Would i really have to specify a rewrite for each different combination of words found in the url?

any help is much appreciated

[edited by: Zacharias at 3:04 am (utc) on May 16, 2007]

3:34 am on May 16, 2007 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

You will probably be a lot happier if you put the hyphens in the original URL (change them within the script you are POSTing to, if possible) so that you do not have to rewrite or redirect to change them.

mod_rewrite is not a full-fledged programming language, and there is no flexibility to handle cases with one, or two, three, or more "+" characters, hyphens, etc., all with one rule. So, an explicit solution for each case will be required:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /folder/index\.php\?s=([a-z]+)\+([a-z]+)\+([a-z]+)\ HTTP/ [NC]
RewriteRule ^index\.php$ http://example.com/folder/%1-%2-%3/? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /folder/index\.php\?s=([a-z]+)\+([a-z]+)\ HTTP/ [NC]
RewriteRule ^index\.php$ http://example.com/folder/%1-%2/? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /folder/index\.php\?s=([a-z]+)\ HTTP/ [NC]
RewriteRule ^index\.php$ http://example.com/folder/%1/? [R=301,L]

Obviously, if you add code for three, four, five, and six hyphens, things start to get a bit inefficient.

mod_rewrite also imposes a limit of nine back-references, so you won't be able to handle more than eight hyphens using this method.

Using the [NC] flag makes character matches case-insensitive and eliminates the need for "[A-Za-z]". It does introduce an ambiguity on the "HTTP/" at the end of the request header, which must always be uppercase in a valid HTTP request, but I wouldn't call that a big hacker target...


9:17 am on May 16, 2007 (gmt 0)

5+ Year Member

Thanks so much for your help on this JD

You are so knowledgeable about a subject that I have to be honest, baffles me sometimes.

Both solutions (fixing in the script and URL redirect) will work nicely for me, Cheers mate :)


Featured Threads

Hot Threads This Week

Hot Threads This Month