I'm using Apache2 and mod_rewrite to hide my query strings. These are the rules in question.
RewriteCond %{QUERY_STRING} ^query=(.*)$
RewriteRule (.*) /search/%1 [R=301,L]
RewriteRule ^search\/?$ /search/?query=test [R=301,L]
When I visit /search (or /search/) I am correctly redirected to /search/?query=test (as per the last rule)
From there, the RewriteCond and RewriteRule should kick in and redirect me to /search/test, right? From what I understand the %1 in my first RewriteRule corresponds to the (.*) in the RewriteCond which should contain test.
However, what actually happens is I'm redirected to /search/test/?query=test. So, the rule works, but for some reason the query string appended. Is it the QSA option being automatically added somehow/somewhere?
I'm then stuck in an infinite loop of redirecting to /search/test?query=test because the first RewriteCond and RewriteRule kick in again, and again, and again...
What am I doing wrong?!
Thanks!
You need to specify an empty query in the substitution to prevent the original requested query to be appended to the new URL:
Modifying the Query String
By default, the query string is passed through unchanged. You can, however, create URLs in the substitution string containing a query string part. Simply use a question mark inside the substitution string to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine new and old query strings, use the [QSA] flag.
So:
RewriteCond %{QUERY_STRING} ^query=(.*)$
RewriteRule (.*) /search/%1? [R=301,L]
Related
Using ISAPI REWRITE v3
I need to replace a string in an URL. The old string is 'p=type1_' and the new is 'p=type2_'. So
http://www.somesite.com/cgi-bin/script.pl?t=something&p=type1_abcde
becomes
http://www.somesite.com/cgi-bin/script.pl?t=something&p=type2_abcde
I reckon this should work:
RewriteRule ^(.*)p=type1_(.*)$ $1p=type2_$2 [NC]
But I get a 'Pattern Not Matched' in the ISAPI_REWRITE RegexTest app as soon as the original string contains a '?' - which, in practice, it always would.
How do I do this simple search and replace?
You will need to use RewriteCond %{QUERY_STRING} to search the querystring.
Try something like this:
RewriteCond %{QUERY_STRING} t=(.*)&p=type1_(.*)
RewriteRule ^cgi-bin/script.pl$ cgi-bin/script.pl?t=$1&p=type2_$2 [NC]
I'm trying to get the following path: /faculty/index.php?PID=FirstLast&type=alpha
To rewrite to this: /faculty/FirstLast
Am I correct to assume the following would be acceptable to put in .htaccess?
# Rewrite old URLS
RewriteCond %{QUERY_STRING} ^PID=([0-9a-zA-Z]*)$
RewriteRule ^/faculty/index.php$ /faculty/%1 [R=302,L]
I'm okay to throw away any other query string variables. I'm applying these rules at the .htaccess file level. This project is a migration from an older system into Drupal.
Outcome:
My .htaccess looks like
# Rewrite old URLS
RewriteCond %{QUERY_STRING} PID=([0-9a-zA-Z]*)
RewriteRule ^faculty/ /faculty/%1/? [R=301,L]
RewriteCond %{QUERY_STRING} vidID=([0-9]*)
RewriteRule ^videos/ /video/id/%1/? [R=301,L]
I also found this wonderful tool -- a mod_rewrite tester
http://htaccess.madewithlove.be/
All good!
Try this instead:
RewriteRule ^faculty/index.php$ /faculty/%1? [R=302,L]
The leading slash is not in the URI-path tested in the rule, so can't be in the regex either.
As the query is automatically appended to the substitution URL (passed through unchanged) unless a new query is created in the rule, the trailing question mark ? erases the existing query string when the rule is used.
My objective is to redirect non-jsp URLs to different subfolder
Browser URL
http://mydomain.com/site-Xghthhg/css/styles1/layouts.css
http://mydomain.com/site-Xghthhg/jslibs/jquery/jquery.js
http://mydomain.com/site-Xghthhg/jslibs/jquery/plugins/jd/jquery.jd.js
Proxy to
http://mydomain.com/map/css/styles1/layouts.css
http://mydomain.com/map/jslibs/jquery/jquery.js
http://mydomain.com/map/jslibs/jquery/plugins/jd/jquery.jd.js
httpd.conf
RewriteCond %{REQUEST_FILENAME} !(.+\.jsp)
RewriteCond %{REQUEST_URI} ^/site-(.*)/^(.*)$
RewriteRule ^/site-(.*)/^(.*)$ /map/$2 [P]
I don't understand why it is not working!
You should be using REQUEST_URI for the first line. You don't need the second line.
Finally, your regex is invalid, the caret means "match the beginning" so it makes no sense to use it in the middle of the regex.
RewriteCond %{REQUEST_URI} !(.+\.jsp)
RewriteRule ^/site-([^/]*)/(.*)$ /map/$2 [P]
Here, I do use a caret in the middle but inside the [] context, caret has a different meaning: match any character except the following (so [^/]* matches a string of characters that are not slashes).
I am trying to use mod_rewrite RewriteRule, and in my RewriteRule, I am trying to match a URL that has query string that looks like:
http:///myfakeoam/obrareq.cgi?....
My RewriteRule looks like:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^wh=(.*)$ [NC]
RewriteRule ^/myfakeoam/obrareq.cgi$ http://apache1.whatever.com/formbasicprotected/index.html [CO=wh:%1:.whatever.com:1440:/]
When I test manually, by manually typing the URL into the browser, that RewriteRule seems to be able match a request if the request looks like:
http:///myfakeoam/obrareq.cgi?wh=xxx&ru=yyyy&....
but, in my actual system, the request is being created by anoher app, and it appears to be URL-encoding (actually URL-encoding twice) the query string (e.g, replacing equal with "%3D", etc.), i.e.:
http:///myfakeoam/obrareq.cgi?wh%3Dxxx....
And if the query string part is URL-encoded like that the pattern match in my RewriteRule is not failing...
Is there any way to handle this situation?
Thanks,
Jim
Answering my own question, I just realized that the RewriteCond was just doing a regex match for the query string, so I changed that to:
RewriteCond %{QUERY_STRING} ^wh%3D(.*)$ [NC]
and then it worked.
Jim
I have a bunch of domains pointing to the same folder, the root of my host "public_html".
I wish I could redirect them, each one for a folder with the same name as the domain.
eg: redirect www.mydomain.com to "public_html/www.mydomain.com"
I tried this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/%{HTTP_HOST}/.*$
RewriteRule ^(.*)$ /%{HTTP_HOST}/$1 [L]
But with no success. What's wrong?
Its even possible to redirect to folder that uses a 'dot' on its name?
Thanks in advanced.
The test pattern in your RewriteCond is actually trying to match input starting with the literal string /%{HTTP_HOST}/. The value of the %{HTTP_HOST} variable is not expanded like you were expecting, so the condition will always be true (the pattern itself will never match). You'll need to modify the RewriteCond, and there are a few different approaches.
If this is the only rewrite you perform, you can just check to see if you've done it yet:
RewriteCond %{ENV:REDIRECT_STATUS} =""
Or, if the resource won't exist until you rewrite it, you can check for that instead:
RewriteCond %{REQUEST_FILENAME} !-f
Finally, you can mimic your original condition by using backreferences within the test pattern, along with a separator character that won't appear in your URL:
RewriteCond /%{HTTP_HOST}/#%{REQUEST_URI} !^([^#]+)#\1
Note that this doesn't guarantee that you've done the redirection though, it only ensures that the request URI has the host name as its first path segment. However, that's generally good enough.