.htaccess catch GET parameters in any order - apache

Im trying to write a rewrite rule that checks if multiple get paramaters are set in any order and then redirect to a url using the arguments from these get parameters this is what I have tried
RewriteCond %{QUERY_STRING} (?:^|&)retailer_filter=([^&]+)
RewriteCond %{QUERY_STRING} (?:^|&)product=([^&]+)
RewriteCond %{QUERY_STRING} (?:^|&)sitechange=([^&]+)
RewriteRule ^$ /?product=%1&retailer_filter=%2 [R=301,L]
This redirects but only has the sitechange argument so
/?product=test&retailer_filter=test&sitechange=1 redirects to ?/product=1&retailer_filter=
when I want it to redirect ?/product=test&retailer_filter=test
but it needs to be able to accept the get parameters in differen orders so
/?sitechange=1t&retailer_filter=test&product=test would also need to redirect to ?/product=test&retailer_filter=test
any help would be appreciated

You are effectively trying to delete sitechange query parameter. You can use this rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} \?(.*&)?sitechange=[^&]*&?(\S*)\sHTTP [NC]
RewriteRule ^/?$ %{REQUEST_URI}?%1%2 [R=301,NE,L]
This rule will accept GET parameters in any order. sitechange= can be at first or last or in middle position.

Related

htaccess redirect using get parameters and remove same parameters

Please help me need redirect search.html?searchword=value&searchphrase=all to search.html?searchword=value
I tried:
RewriteCond %{QUERY_STRING} ^searchword=(.*) [NC]
RewriteCond %{QUERY_STRING} searchphrase= [NC]
RewriteRule ^(.*)$ /search.html\?searchword=%1 [R=301,L]
but it do not work.
You may use it like this:
RewriteCond %{QUERY_STRING} (?:^|&)searchphrase= [NC]
RewriteCond %{QUERY_STRING} (?:^|&)(searchword=[^&]*) [NC]
RewriteRule ^search\.html$ %{REQUEST_URI}?%1 [R=301,L,NC,NE]
Changes are:
Order of RewriteCond is important. Keep the condition with capture group as last one
No need to repeat searchword again in target, just capture from RewriteCond and use it later as back-reference %1
Instead of using .* use [^&]* to match only value till you get next & or end of string
Match search.html in rule pattern to avoid matching anything else
With your shown samples and attempts please try following .htaccess rules. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(search\.html)\?(searchword=[^&]*)&searchphrase=all [NC]
RewriteRule ^ /%1?%2 [R=301,L,NE]

How to combine .htaccess rules?

I want to combine these two .htaccess rules for a website:
RewriteRule ^newsletter\.html$ https://fleio.com/newsletter [L,R=301]
RewriteRule ^newsletter[/]?$ newsletter.html [L]
If only one is active, it is doing what it has to do. But when I use both of them I get an error
too many redirects.
https://docs.google.com/document/d/1M6rYkEV-tmO7wRSJYjGtGOFow-76ZvbODnnjiqCh1m0/edit?usp=sharing
Your second rule is internally redirecting the request to same location (The first rue) multiple time so you are getting the loop error. You need to use a RewriteCond and match against the_request variable to prevent the rewrite loop.
Try this :
RewriteEngine on
RewriteCond %{THE_REQUEST} /newsletter\.html [NC]
RewriteRule ^ /newsletter/ [L,R]
RewriteRule ^newsletter/?$ /newsletter.html [NC,L]

Restrict some url pattern in a apache redirect

I am writing a redirect to match any url of the patter /message/* here.
RewriteRule ^/message/(.+)$ http://abet.in/message/$1 [NC,R=301,L]
Now I want to modify it by not allowing some string pattern in url.
RewriteCond to check /message/index.html in the url.
1. Check if the request url contains /message/index.html.
2. If the condition is not met then do a redirect.
I tried the following methods. But I am not sure whether they are correct or not.
RewriteCond %{THE_REQUEST} !^/message/index
RewriteCond %{REQUEST_URI} !^/message/index [NC]
Could some one tell how to do this.
%{THE_REQUEST} contains a string that looks like this for a regular page request:
GET /message/index.html HTTP/1.1
And %{REQUEST_URI} looks like this:
/message/index.html
So your 2nd option is almost correct. You don't need a / at the start of the Pattern for RewriteRules.
Additionally, these two rules will prevent all requests that start /message/index from being redirected):
RewriteCond %{REQUEST_URI} !^/message/index [NC]
RewriteRule ^message/(.+)$ http://abet.in/message/$1 [NC,R=301,L]
If you only want to prevent /message/index.html and not /message/index.php or /message/index-of-something-else then do:
RewriteCond %{REQUEST_URI} !^/message/index\.html [NC]
RewriteRule ^message/(.+)$ http://abet.in/message/$1 [NC,R=301,L]

redirect url with query string to path, and url without query string must be internally rewritten

I've been trying and trying.
If one goes to:
www.domain.nl/vereniging
internally a page is requested from:
www.domain.nl/?p=vereniging
For that I use this:
RewriteCond %{QUERY_STRING} !(p=.*)$
RewriteRule ^(.+)$ ?p=$1 [NC]
If a users visits:
www.domain.nl/?p=vereniging
I want the users to be redirected to:
www.domain.nl/vereniging
For that I use:
RewriteCond %{QUERY_STRING} ^p=(.*)$ [NC]
RewriteRule ^$ http://www.domain.nl/%1? [NC,R=301]
(If I put RewriteCond %{REQUEST_FILENAME} !-d before this, it doesn't redirect anymore. That's strange because a query is not a directory right?)
Separately, these 2 chunks of code work.
However, if I put them together in 1 .htaccess it bitches about looping.I don't understand this, because the conditions should prevent looping.
Try applying the END flag to either the first or second RewriteRule.
Look at the END flag here: http://httpd.apache.org/docs/current/rewrite/flags.html
You need to check against the actual request:
RewriteCond %{THE_REQUEST} \?p=([^&\ ]+)
RewriteRule ^ /%1? [L,R=301]

Redirect in combination with a rewrite condition in htaccess

I've got a cms-driven website, with no option to change the code. What I want to accomplish is creating friendly url's, using only apaches mod-rewrite engine.
The problem is I'm creating an infinite loop, because I first redirect the original url (index.php?id=21) to a friendly one (/friendly/) and then rewrite the '/friendly' part back to 'id=21'
I know there should be an extra condition or parameter to avoid looping in this case, but I can´t get one of the possible solutions to work.
Here´s the code:
RewriteCond %{query_string} ^id=21$ [NC]
RewriteRule /* /peuterspeelzaal? [R=301,L]
RewriteRule ^peuterspeelzaal$ index.php?id=21 [L]
You need to look at the request line in THE_REQUEST to see what originally has been requested:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?id=21\ [NC]
RewriteRule /* /peuterspeelzaal? [R=301,L]
RewriteRule ^peuterspeelzaal$ index.php?id=21 [L]
I'm guessing you are rewriting in both directions to ensure that old links are redirected to the new friendly urls?
You could just add a dummy parameter to all your "friendly" rewrites so that they don't match the other rule:
RewriteCond %{query_string} ^id=21$ [NC]
RewriteRule /* /peuterspeelzaal? [R=301,L]
RewriteRule ^peuterspeelzaal$ index.php?id=21&dummy=1 [L]