Forum Moderators: phranque
Am a little confused about a htaccess redirect.
I am wondering if it is possible to do this?
Redirect this
http://example.com/index.php?s=films&c=dvds
to
http://example.com/films/dvds/
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.
Z
[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]
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]
<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]
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]
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]
Jim
[edited by: jdMorgan at 6:09 pm (utc) on May 15, 2007]
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
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$ [127.0.0.1...] [R=301,L]
This is not working as expected and is giving me:
/cheap+action+dvds/
insted of
/cheap-action-dvds/
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]
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]
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...
Jim