I would like to convert a single lowercase letter into an uppercase letter during rewrite in .htaccess. For example, I have this
URL: www.domain.com/?country=CUoatia
and I wanted it to become: www.domain.com/Croatia
In addition, the uppercase letter 'U' will be converted to 'r'.
I can achieve www.domain.com/CUoatia using the following code:
RewriteCond %{QUERY_STRING} ^country=(.*)
RewriteRule ^$ %1? [R=301]
But I do not know how to change that 'U' character to 'r'.
You can try changing your rules around to:
# Replace all capital U in query string
RewriteCond %{QUERY_STRING} ^(.*)U(.*)$
RewriteRule ^(.*)$ /$1?%1r%2 [L]
# Make sure there are no capital U in the query string
RewriteCond %{QUERY_STRING} !U
RewriteCond %{QUERY_STRING} ^country=(.*)
RewriteRule ^$ %1? [R=301]
Related
Hy, I have this rewriteRule to change a /search?key=ok
to /search/ok
from a search bar
RewriteCond %{QUERY_STRING} key=([0-9a-zA-Z_-]+) [NC]
RewriteRule (.*) /search/%1? [R=302,L]
But when I search something with spaces like ok ko
it's just return /search/ok.
What do I have to change to have /search/ok-ko ?
Thanks !
Your regular expression pattern ([0-9a-zA-Z_-]+) matches only alphanumeric characters , you can add \s to your pattern to match a space character in Querystring
RewriteCond %{QUERY_STRING} key=([0-9a-zA-Z\s_-]+) [NC]
RewriteRule (.*) /search/%1? [R=302,L]
I Rewrite a url via htaccess but if I write it with lowercase or not exactly us it is inside the RewriteRule won't work, returns that the file or object not found. Example if I write go to url ACTIVATE/TheTokenKey returns not found. Is there anything that will read the url to lowercase, So if we write AcTivAtE will read it as activate.
RewriteBase /
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^Activation/(.*)$ ?tab=activation&token=$1 [L]
Use the [NC] flag on your rule
RewriteRule ^Activation/(.*)$ ?tab=activation&token=$1 [NC,L]
Use of the [NC] flag causes the RewriteRule to be matched in a
case-insensitive manner. That is, it doesn't care whether letters
appear as upper-case or lower-case in the matched URI.
https://httpd.apache.org/docs/current/rewrite/flags.html
This is really bugging me, I have followed a few tutorials but just can't get anywhere. I'm trying to do a 301 redirect from:
/webpage-mackay.php?wp=Mission
to:
http://domain.org.au/webpage.php?wp=Mackay%20Mission
I have attempted writing like this:
RewriteCond %{QUERY_STRING} ^wp=Mission$
RewriteRule ^/webpage-mackay\.php$ http://domain.org.au/webpage.php?wp=Mackay%20Mission [R=301,L]
and:
RewriteCond %{REQUEST_URI} ^/webpage-mackay.php$
RewriteCond %{QUERY_STRING} ^wp=Mission$
RewriteRule ^(.*)$ http://domain.org.au/webpage.php?wp=Mackay%20Mission [R=301,L]
But the result is:
http://domain.org.au/webpage.php?wp=Mission
Am I missing something? I have used this and this as a reference
I see 2 problems with your first attempt : there is no need for the leading slash in the RewriteRule, and the %20 doesn't work as "%" is a special character.
Here what you can try :
# Solution 1 : with a space character in the final URL
RewriteCond %{QUERY_STRING} ^wp=Mission$
RewriteRule ^webpage-mackay\.php$ http://domain.org.au/webpage.php?wp=Mackay\ Mission [R=301,L]
# Solution 2 : with the %20 in the final URL
RewriteCond %{QUERY_STRING} ^wp=Mission$
RewriteRule ^webpage-mackay\.php$ http://domain.org.au/webpage.php?wp=Mackay\%20Mission [R=301,L,NE]
How can i replace + to - in my url. What code shoud I add in my htacces to get rid of + and replace it with a minus.
RewriteCond %{QUERY_STRING} ^search=([^&]+)$
RewriteRule ^ http://mysite.com\/Download\/free\/%2.html? [R,L,NE]
If i type a search that contain spaces, every space become + and i want -.
I'm assuming that the bla+bla+bla part of the URL: http://mysite.com/Download/free/bla+bla+bla.html originated from the query string search='s value. Depending on what other rules you may have, you can go about this in 2 different ways. You can either remove all of the spaces from the query string first, before it's redirected to the .html file. Or you can rewrite the query string into the URI, then remove the spaces before redirecting. It'll be something like this:
RewriteCond %{QUERY_STRING} ^search=(.*?)(\+|%20)(.*)$
RewriteRule ^ /?search=%1-%3 [L,NE]
RewriteCond %{QUERY_STRING} !(\+|%20)
RewriteCond %{QUERY_STRING} ^search=([^&]+)$
RewriteRule ^ http://mysite.com\/Download\/free\/%1.html? [R,L,NE]
Note that in your htaccess you have the %2 back reference, which doesn't seem to reference anything.
Or rewrite to URI first, then redirect:
RewriteCond %{QUERY_STRING} ^search=([^&]+)$
RewriteRule ^ /Download\/free\/%1.html? [L,NE]
RewriteCond %{REQUEST_URI} !(\ )
RewriteRule ^ - [L,R]
RewriteRule ^(.*)\ (.*)$ /$1-$2 [L]
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