I want to match PHPSESSID with query string part (?session_id=b0300e9317e626da2c3f9a45e28b5106) of a URL. But i am not able to do this. I think the second rule does not seem to work. What's my wrong?
RewriteCond %{HTTP_COOKIE} !PHPSESSID=(\w+) [NC]
RewriteCond %{HTTP_COOKIE}:%{QUERY_STRING} ^PHPSESSID=(.*?);:(?!.*?session_id=\1).* [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/? [R=302,L,NC]
Missing OR flag on first RewriteCond and both conditions are broken. It should be something like this:
RewriteCond %{HTTP_COOKIE} !(?:^|\s)PHPSESSID= [OR]
RewriteCond %{HTTP_COOKIE};;%{QUERY_STRING} !(?:^|\s)PHPSESSID=([^;]+).*?;;(?:.*?&)?session_id=\1(?:$|&)
RewriteRule ^ https://%{HTTP_HOST}/? [R=302,L]
Related
I'm trying to redirect using .htaccess, from example.com/products/category/subcat/name/?page=0 to example.com/example-subpage
my code not working:
RewriteCond %{REQUEST_URI} ^/products/category/subcat/name$
RewriteCond %{QUERY_STRING} ^page=0$
RewriteRule ^ https://example.com/example-subpage [R=301,L,QSD]
The issue probably is the handling of the trailing slash in your first condition. You'd need to include it into the condition:
RewriteCond %{REQUEST_URI} ^/products/category/subcat/name/$
RewriteCond %{QUERY_STRING} ^page=0$
RewriteRule ^ https://example.com/example-subpage [R=301,L,QSD]
I personally would always prefer a slightly more flexible matching with an optional trailing slash:
RewriteCond %{REQUEST_URI} ^/products/category/subcat/name/?$
RewriteCond %{QUERY_STRING} ^page=0$
RewriteRule ^ https://example.com/example-subpage [R=301,L,QSD]
That could be simplified to this:
RewriteCond %{QUERY_STRING} ^page=0$
RewriteRule ^/?products/category/subcat/name/?$ https://example.com/example-subpage [R=301,L,QSD]
And last not least it might make sense to add a bit of flexibility to the remaining condition to also match if other, unexpected arguments get added:
RewriteCond %{QUERY_STRING} (?:^|&)page=0(?:&|$)
RewriteRule ^/?products/category/subcat/name/?$ https://example.com/example-subpage [R=301,L,QSD]
I have this code in my .htacces file:
RewriteCond %{QUERY_STRING} \\?s=([^&]+) [NC]
RewriteRule ^$ job\?search_keywords=%1 [NC,R,L]
And it works well for: example.com/?s=blabla
Now I would like to add that if the url contains /en/:
https://example.com/en/?s=blabla
change for https://example.com/en/work/?search_keywords=blabla
Somone could help me with it? Because my trying gives me ERROR 500.
You just need another rule to handle new URI pattern:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^s=([^&]+) [NC]
RewriteRule ^$ /job?search_keywords=%1 [R=301,L]
RewriteCond %{QUERY_STRING} ^s=([^&]+) [NC]
RewriteRule ^en/?$ /en/work/?search_keywords=%1 [R=301,L]
Note that \\? in your RewriteCond is redundant as QUERY_STRING variable doesn't contain ?
I want use below link and load from category.php but it does not work
http://test.com/?search=x
.htaccess:
RewriteRule ^search$ category.php?search=$1 [QSA,NC]
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/test\.com$
RewriteCond %{QUERY_STRING} ^search=(.*)$
RewriteRule ^(.*)$ http://category.php?search=%1 [R=302,L]
The reason you can't get away with a single RewriteRule is that query parameters cannot be directly rewritten. Instead, the above code captures the query string using RewriteCond %{QUERY_STRING}, which is then made available as %1 in the RewriteRule which follows. The RewriteCond %{REQUEST_URI} ensures that the code will only evaluate for the test.com domain.
Try this,
RewriteEngine On
RewriteCond %{QUERY_STRING} ^search=(.*)$
RewriteRule ^(.*)$ category.php?search=%1 [QSA,NC,L]
You can use this simple rule:
RewriteCond %{QUERY_STRING} ^search= [NC]
RewriteRule ^/?$ category.php [L]
Pattern ^/?$ ensures that we match only landing page, nothing else.
QUERY_STRING is automatically appended to target URI so there is no need to capture and append in target URI.
I need to redirect
http://example.com/page/?Id=1253-23098-
to
http://example.com/page/1253-23098
This redirection also includes removal of '-' from end of query string.
What i've done is
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/page/$
RewriteCond %{QUERY_STRING} ^Id=([0-9\-\0-9]*)$
RewriteRule ^(.*)$ http://example.com/page/%1? [L,R=301]
which is redirecting me to
http://example.com/page/1253-23098-
I need to removed anything after "1253-23098" from my query string.
I've googling it from last 3-4 hours buts nothing seems to work.
Any help is appreciated.
You can use:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^Id=(\d+-\d+) [NC]
RewriteRule ^page/?$ page/%1? [NC,L,R=301]
Try :
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/page/?$
RewriteCond %{QUERY_STRING} ^Id=([0-9]*)-([0-9]*)-$
RewriteRule ^(.*)$ http://example.com/page/%1-%2? [L,R=301]
I'm having an issue with a rather old siye. I have some generic URL's with a query string, that i want to 301 redirect, but I don't want to blanket re-direct the urls. I want to choose where each query string is being redirected as there are alot of different categories within the site. For example:
I want to change:
index.php?_a=viewCat&catId=199
to:
/garden-furniture/patio-furniture/garden-benches-garden-seats/cat_199.html
But ill want to change another catid to another URL of my choosing, completely different structure. The problem I'm having is with the code i've got, if I don't have a ? at the end of the destination url, it works, but appends the query string to the end, if I put it on the end, it doesn't redirect at all.
Code I'm using:
RewriteCond %{QUERY_STRING} ^_a=viewCat&catId=199
RewriteRule ^index\.php$ /garden-furniture/patio-furniture/garden-benches-garden-seats/cat_199.html? [L,R=301]
Any help would be appreciated!
EDIT: The rest of my htaccess
RewriteEngine On
RewriteRule ^conservatory/(.*)$ /conservatory-furniture/$1 [R=301,L]
RewriteRule ^dining-room/(.*)$ /dining-room-furniture/$1 [R=301,L]
RewriteRule ^garden/(.*)$ /garden-furniture/patio-furniture/$1 [R=301,L]
RewriteBase /
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule cat_([0-9]+)(\.[a-z]{3,4})?(.*)$ index.php?_a=viewCat&catId=$1&%1 [NC]
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule prod_([0-9]+)(\.[a-z]{3,4})?$ index.php?_a=viewProd&productId=$1&%1 [NC]
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule info_([0-9]+)(\.[a-z]{3,4})?$ index.php?_a=viewDoc&docId=$1&%1 [NC]
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule tell_([0-9]+)(\.[a-z]{3,4})?$ index.php?_a=tellafriend&productId=$1&%1 [NC]
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule _saleItems(\.[a-z]+)?(\?.*)?$ index.php?_a=viewCat&catId=saleItems&%1 [NC,L]
If these are complete URLs, you could anchor the pattern at the start of the string
RewriteRule ^cat_([0-9]+)(\.[a-z]{3,4})?(.*)$ index.php?_a=viewCat&catId=$1&%1 [NC]
this would prevent an URL, which has cat_ inside being rewritten to index.php?....
And since you don't use the trailing optional part, you could eliminate this too
RewriteRule ^cat_([0-9]+) index.php?_a=viewCat&catId=$1&%1 [NC]
Another point is the RewriteCond with query string. If the query string is optional, you could remove the RewriteCond and modify the RewriteRules to
RewriteRule ^cat_([0-9]+) index.php?_a=viewCat&catId=$1 [QSA,NC]
So, all these would become
RewriteRule ^cat_([0-9]+) index.php?_a=viewCat&catId=$1 [QSA,NC]
RewriteRule ^prod_([0-9]+) index.php?_a=viewProd&productId=$1 [QSA,NC]
RewriteRule ^info_([0-9]+) index.php?_a=viewDoc&docId=$1 [QSA,NC]
RewriteRule ^tell_([0-9]+) index.php?_a=tellafriend&productId=$1 [QSA,NC]
RewriteRule ^_saleItems index.php?_a=viewCat&catId=saleItems [QSA,NC,L]