My client wants a query string munged (by changing % to A) on certain pages.
For example, I can remove the query string completely on the desired pages via:
RewriteCond %{QUERY_STRING} !=""
RewriteCond %{REQUEST_URI} ^/SpecialPage(.*)
RewriteRule ^(.*)$ /$1? [R=301,L] #remove query string
Here's what I thought should remove % on the query string and replace with A but it's not:
RewriteCond %{QUERY_STRING} ^(.*)\%(.*)$
RewriteCond %{REQUEST_URI} ^/SpecialPage(.*)
RewriteRule ^(.*)$ /$1?%1A%2 [L]
What am I doing wrong in this? I just can't quite spot it. Thanks for the expert eyes!
You're real close.
The problem here is that you've got a condition and the match of your rule should be together. Your backreference to the previous RewriteCond is broken because it's for the REQUEST_URI and not the QUERY_STRING like you want.
RewriteCond %{REQUEST_URI} ^/SpecialPage(.*)
RewriteRule ^(.*)$ /$1?%1A%2 [L]
Here, the %1 backreference matches the (.*) at the end of the /SpecialPage URI. The backreferences from your query string match gets lost, and that's the ones you really want. You can combine the condition to match the REQUEST_URI with the regular expression pattern in the RewriteRule:
RewriteCond %{QUERY_STRING} ^(.*)\%(.*)$
RewriteRule ^SpecialPage(.*)$ /SpecialPage$1?%1A%2 [L]
Here, the %1 and %2 backreferences correctly reference the query string and the SpecialPage condition in the URI is met by the regex pattern.
Redirect Query String
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.)=(.)$
RewriteRule ^(.)?(.)$ /$1--%0? [R=301,L]
From Url: http://localhost/sholay-slide.jsp?slide=2
To Url: http://localhost/sholay-slide.jsp--slide=2
Related
I've got a buggy query string in my url so it appears in the beginning of a url:
https://website.com/?lang=en/wp-content/uploads/2019/10/image.png
How can I remove this query string so that the url redirects to https://website.com/wp-content/uploads/2019/10/image.png?
I've tried the following rules to no avail:
RewriteEngine On
RewriteCond %{QUERY_STRING} "lang=" [NC]
RewriteRule ^(.*) /$1? [R=301,L]
Any help appreciated. Thank you!
The path component of your URL is empty here, so RewriteRule ^(.*) will only capture an empty string, and that means $1 will be empty as well.
The info you are looking for is in the query string - so you have to capture it from there:
RewriteEngine On
RewriteCond %{QUERY_STRING} lang=(.*) [NC]
RewriteRule . /%1? [R=301,L]
%1 instead of $1, because that is a back reference not to the RewriteRule pattern, but to the Condition.
Now this might lead to unexpected results if your query string could ever contain more GET parameters after lang. In that case, you might have to be a bit more specific with your pattern (like try to match anything after lang, that is not an ampersand - lang=([^&]*))
So currently I have URLs that looks like this:
http://localhost/?v=register
http://localhost/?v=profile&u=thatgerhard
I want the above to look like this:
http://localhost/register/ (without trailing /)
http://localhost/profile/thatgerhard/ (without trailing /)
I spent a ton of time trying to get this to work even though it seems like it should be a simple fix.
This is what I have atm:
RewriteBase /
RewriteEngine On
RewriteRule ^((.)*)$ /?v=$1&p=$
I would ideally like this to be dynamic so that if you do ?v=foo it will automatically do /foo/ without adding "foo" to the htaccess file.
Any help or direction will be greatly appreciated. :)
You should use RewriteCond Directive and RewriteCond backreferences. The %1 backreference matches the query string parameter v, the %2 backreference matches the query string parameter u. The RewriteRule redirects the request permanently and discard the query string entirely.
RewriteCond %{QUERY_STRING} ^v=([^&]*)$ [NC]
RewriteRule ^/?$ /%1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^v=([^&]*)&u=([^&]*)$ [NC]
RewriteRule ^/?$ /%1/%2/? [R=301,L]
is this query correct for redirecting example.com/?abcd2 to example.com/abcd2.php
RewriteCond %{QUERY_STRING} ^?abcd2$
RewriteRule (.*) http://example.com/abcd2.php [R=301,L]
No there are some minor glitches. Use this rule:
RewriteCond %{QUERY_STRING} ^abcd2$
RewriteRule ^ http://example.com/abcd2.php? [R=301,L]
QUERY_STRING value doesn't start with ? and you need ? in target URI to strip off existing query string.
Trying to redirect index.php?page=11. (with the period) to index.php?page=11 (no period). This is a page that somehow was indexed in Google, that breaks with the period in the URL.
RewriteRule ^index\.php\?page\=11\.$ index.php?page=11 [R=301]
This is not catching.
You can't match against the query string (everything after the ?) in a rewrite rule, you need to match against the %{QUERY_STRING} variable in a condition:
RewriteCond %{QUERY_STRING} ^page=11\.$
RewriteRule ^index\.php$ /index.php?page=11 [L,R=301]
I am trying to make:
http://www.specialisedorthoticservices.co.uk/image.php?object_type=detailed&image_id=140&window=popup
become this:
http://www.specialisedorthoticservices.co.uk
The result of the query no longer exists and the re-direct doesn't seem to work see code below:
RewriteCond %{REQUEST_URI} ^/image\.php$
RewriteCond %{QUERY_STRING} ^object_type=detailed&image_id=140&window=popup$
RewriteRule ^(.*)$ http://www.specialisedorthoticservices.co.uk [R=301,L]
Remove the slash before image.php. In fact, why not just shorten it? To not append the query string you need a terminating question mark like so
RewriteEngine On
RewriteCond %{QUERY_STRING} ^object_type=detailed&image_id=140&window=popup$
RewriteRule ^image\.php$ /? [R=301,L]