Forum Moderators: phranque
When a client(or robot) enters a url in the form:
www.example.com/cgi-bin/webscript.pl?page=rrrr&cart_id=ssss
I want a redirect to www.example.com/rrrr
Im using:
RewriteCond %{REQUEST_URI} webscript.pl
RewriteRule ^([^=]+)=([^&]+)&(.*)$ www.example.com/%2 [R=301,L]
but with no luck
The .htaccess file is under the cgi-bin directory.
The only thing I got to work, is redirecting to the home
page using:
RewriteCond %{REQUEST_URI} webscript.pl
RewriteRule ^(.*)$ www.example.com/ [R=301,L]
but as soon as i start playing around with the regular expression manipulation in the query portion of the string, everything breaks down, as if the query string is not visible to the rewriterule.
Any sugestions?
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /webscript\.pl\?([^&]+&)*page=([^&]+)
RewriteRule ^webscript\.pl$ http://www.example.com/%2 [R=301,L]
GET /webscript.pl?prod=widget&color=blue&texture=fuzzy HTTP/1.1
The code, while appearing to be somewhat redundant, is what is required to prevent the looping mentioned above.
Jim
You are close with the condition, but will need to use THE_REQUEST to match a request and prevent looping if you are still using the dynamic page(s) to serve your content from.
This can range from complicated to very complicated depending on how many variables you are passing on the dynamic pages.
The problem you are having with the query string is Apache does not treat information after the? as part of the URL, because it is technically data *not* location information.
If you do not need to use the dynamic pages to serve information, you can simply use a QUERY_STRING condition to match information after the?
RewriteCond %{QUERY_STRING} ^[^=]+=([^&]+)&
RewriteRule ^webscript.pl$ http://www.example.com/%1 [R=301,L]
By not 'end anchoring' ($) the condition, you can use the implicit 'and everything else...' rather than having to match a pattern. You can also remove the first back-reference since it does not appear you need to use it.
One of the problems you may have been running into is %1 = Condition back-refernece, while $1 = Rule back-reference.
There are some good examples around for the use of THE_REQUEST, but I am not sure if you will be needing to use it, so please, let us know how things work out, or if you get stuck...
Hope this helps.
Justin
ADDED: There is actually a nice example of THE_REQUEST above... Sometimes I think Jim checks to see if I am online and makes sure he posts his answer first =) Hey again Jim
I also added a question mark after %1 to dump the query string as I don't need it, and without the question mark after %1, the query appears to get appended onto the static string.
I had to change the name of my main script also on the website, as I had two .htaccess files, one for going static to dynamic and the other for going dynamic to static. Was causing an infinite loop, on my test server, but that was an easy fix.
One thing in the first example by JDMorgan, what is the purpose of the {3,9}. I know its say match the regular expression from 3 to 9 times, but why is it important?
Thanks for the input.
> [...] causing an infinite loop
You might want to review my comments about looping, also in that first post.
Jim