htaccess redirect using get parameters and remove same parameters - apache

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]

Related

Apache HTTP_URI redirect everything but sertain urls that query_string

We would like to redirect all trafic to our new page. We would still like that ceratin urls are still accessible on our old page, because these are used by our partners and are not yet ready to migrate.
The problem i'm having is with QUERY_STRING parameters.
My rules so far:
RewriteCond %{REQUEST_URI} !^/endfile.php
RewriteCond %{REQUEST_URI} !^/sites/default/files/pdffolder/
RewriteCond %{REQUEST_URI} !^/insure/agency
# RewriteCond %{REQUEST_URI} !^/content/insurance1?szs=000029&wssl=1
# RewriteCond %{REQUEST_URI} !^/text/insurance2?wssl=1&zst=enter
RewriteRule (.*) https://www.new-domain.eu/ [R=301,L]
The code works for the fist 3. I cant get it to work for the 4th and 5th rule. I currently have the commented out.
I tried using QUERY_STRING rules, without success. Is htere any way to use AND in rewrite condition?
Any ideas?
You cannot match query string using REQUEST_URI. Use THE_REQUEST to match both URI and query:
RewriteCond %{REQUEST_URI} !^/endfile\.php [NC]
RewriteCond %{REQUEST_URI} !^/sites/default/files/pdffolder/ [NC]
RewriteCond %{REQUEST_URI} !^/insure/agency [NC]
RewriteCond %{THE_REQUEST} !\s/+content/insurance1\?szs=000029&wssl=1[&\s] [NC]
RewriteCond %{THE_REQUEST} !\s/+text/insurance2\?wssl=1&zst=enter[&\s] [NC]
RewriteRule ^ https://www.new-domain.eu/? [R=301,L]
Also note use of trailing ? in target to strip off any existing query string.

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]

Rewriting every occurrence of pattern in query string via mod_rewrite

I have queries of this form going towards my server:
http://my.host.com/moo?p=1&s=2&targetURL=http://foo.com/sub/more/query
What I need to do is replace a part of the parameter of this query, specifically I need to replace http://foo.com/sub/ with http://bar.com/, preserving all the other parameters in this query and also preserving the parameters of http://foo.com/sub/more/query. I would be content with just replacing it in targetURL, so I tried this:
RewriteCond %{REQUEST_URI} ^/moo
RewriteRule ^/moo(.*)targetURL=http://foo.com/sub(.*)$ http://other.host.com/moo$1targetURL=http://bar.com$2 [L,R]
RewriteRule ^/moo(.*)$ http://other.host.com/moo$1 [L,R]
But the first query just never matches. Any help?
EDIT: To make clear, I have this:
http://my.host.com/moo?p=1&s=2&targetURL=http://foo.com/sub/more/query
and I want it to become this:
http://other.host.com/moo?p=1&s=2&targetURL=http://bar.com/more/query
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^my\.host\.com [NC]
RewriteCond %{QUERY_STRING} ^(.*)targetURL=http://foo.com/sub/(.*)$
RewriteRule ^/?moo$ http://other.host.com/moo?%1targetURL=http://bar.com/%2 [L,R=301]
You need to match the targetURL=http://foo.com/sub/ part from within the %{QUERY_STRING} variable using a RewriteCond. The query string isn't part of the URI when a RewriteRule matches against it. You can then use the %1 and %2 back references to reference the groupings matched in the query string.
Try this version which i tested and works for me.
<i>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^my\.host\.com/moo [NC,OR]
RewriteCond %{QUERY_STRING} ^p\=1\&s=2\&targetURL\=http\:\/\/foo.com/sub/more/query$ [NC,OR]
RewriteRule ^/(.*) http://other.host.com/moo?p=1&s=2&targetURL=http://bar.com/more/query/$1 [R=301,L]
</IfModule>

how do i get mod_rewrite to execute the RewriteRules if and only if there is no parameter q= in the url?

the below code works to deal with my url structures but i need the rules not to work if there is parameter q= in the url.
i tried to set up a rewritecond (in the commented out line below) without success,
please help! thanks :)
Options +FollowSymlinks
RewriteEngine on
# RewriteCond %{QUERY_STRING} q!=(.*)
RewriteRule ^FSD/([^/]+)/([^/]+)/ /index.php?service=$1&type=FSD [NC]
RewriteRule ^ECD/([^/]+)/([^/]+)/ /index.php?service=$1&type=ECD [NC]
RewriteRule ^([^/]+)/([^/]+)/ /index.php?category=$1&subcategory=$2 [NC]
RewriteRule ^([^/]+)/ /index.php?category=$1 [NC]
Your RewriteCond's test pattern is a little off, but you got the right idea:
RewriteCond %{QUERY_STRING} !(\A|&)q=.*
Note that the RewriteCond only applies to the next RewriteRule, so if you want to perform this exclusion for all four of your rules, you'll need to copy the RewriteCond above each of them.

Strip specific parameteters when redirecting with Mod-Rewrite

I have a pretty complex RewriteRule where I need to check if certain parameters are present in QueryString and then redirect to the same URL but with those parameters stripped.
How can I remove some parameters and preserve the rest?
RewriteCond %{QUERY_STRING} color=red
RewriteCond %{QUERY_STRING} status=contiue
RewriteRule ^(.*)$ /$1? [R=301,L]
url is like:
"http://example.com/site.php?setup=done&color=red&weight=100&status=continue"
(parameters order and quantity is not predictable/hardcoded)
Try these rules:
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)(color=red|status=continue)($|&)(.*)
RewriteRule .* $0?%1%5 [N,E=REMOVED:true]
RewriteCond %{ENV:REMOVED} true
RewriteRule ^ %{REQUEST_URI} [L,R=301]
Another way would be to use PHP to check what parameters are given and remove them there.