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]
Related
I'm moving an old site to a new platform and the query strings have changed so looking to 301 them but no luck yet... so an old search string by state for example I'm using:
RewriteCond %{QUERY_STRING} ^a=19&b[yes_state]=(&.*)?$ [NC]
RewriteRule ^c/system\.php$ https://thenewwebsite.com/?cp_search=1&spost_region=%1 [R=301,NE,NC,L]
to basically send:
theoldwebsite.com/c/system.php?a=19&b[yes_state]=Texas
to
thenewwebsite.com/?search=1&t_region=Texas
In regular expression [ and ] are special characters ,you need to escape them by using a backslash in front to match them litterlly.
RewriteCond %{QUERY_STRING} ^a=19&b\[yes_state\]=(.*)$ [NC]
RewriteRule ^c/system\.php$ https://thenewwebsite.com/?cp_search=1&spost_region=%1 [R=301,NE,NC,L]
How would i redirect this url to www.example.com using a htaccess file.
used-machinery/en/other/used-machinery-item?000172:compost-turner
Would this work?
RewriteRule ^used-machinery/en/other/used-machinery-item?000172:compost-turner?$ http://www.example.com/ [R=301,L]
RewriteRule ^(.*)$ index.php/$1 [L]
Would the ? cause an issue in the old url?
The question mark should represent a query string, thus you would have to make write a condition to detect the query string. This should work
RewriteEngine On
ReWriteCond %{REQUEST_URI} ^/used-machinery/en/other/used-machinery-item$
ReWriteCond %{QUERY_STRING} ^000172:compost-turner$
RewriteRule ^(.*)$ http://www.example.com/ [R=301,L]
RewriteRule ^(.*)$ index.php/$1 [L]
Edit:
With a huge bunch or URLs you can use the "OR" flag or perhaps even try something on the server side. It would be a pain but I don't know any other solution with a huge amount of links.
RewriteEngine On
# %{THE_REQUEST} format as follows
# GET /used-machinery/en/other/used-machinery-item?000172:compost-turner HTTP/1.1
# important notes:
# escape ? with( \?)
# space after url (\ )
# each line has [NC,OR] flags for case insensitive and checks this condition or the next
# last Cond line does not have the OR flag
ReWriteCond %{THE_REQUEST} ^.+\ /used-machinery/en/other/used-machinery-item\?000172:compost-turner\ .*$ [NC,OR]
ReWriteCond %{THE_REQUEST} ^.+\ /anoterURL\?querystring\ .*$ [NC,OR]
ReWriteCond %{THE_REQUEST} ^.+\ /lastURL\?with-no-OR-at-end\ .*$ [NC]
RewriteRule ^(.*)$ http://www.example.com/ [R=301,L]
RewriteRule ^(.*)$ index.php/$1 [L]
If you wish to a a mass edit, I suggest using Sublime Text
Paste all links with one link per line
Turn word wrap off
Select a ? in a URL
In the menu, Find > Quick Find All (to select all ?s on the page)
Press left cursor arrow
type the \
Press the End key to go the end of all lines
type \ .*$ [NC,OR]
Press the Home key to go the beginning of all lines
type ReWriteCond %{THE_REQUEST} ^.+\
Edit the last row to remove the ,OR
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} 000172:compost-turner [NC]
RewriteRule ^used-machinery/en/other/used-machinery-item/?$ /? [R=301,L]
RewriteRule ^((?!index\.php/).*)$ index.php/$1 [L,NC]
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
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]