Appending to query string with everything intact - apache

I tried the below. That doesn't seem to work.
If the referrer has the string google.com, I want the rewrite rule to trigger, and preserve the existing query string and just append the referrer to the end of the query string
RewriteEngine On
RewriteCond %{HTTP_REFERER} .*google.com.*$
RewriteRule ^(.*)$ ^(.*)$referrer=%{HTTP_REFERER} [QSA]
I'm sure something in my rewriterule is wrong. Can someone point me in the right direction?

This should be better for you:
RewriteEngine On
RewriteCond %{QUERY_STRING} !(?:^|&)referrer=
RewriteCond %{HTTP_REFERER} google\.com
RewriteRule ^(.*)$ $1?referrer=%{HTTP_REFERER} [QSA]

This worked
RewriteEngine On
RewriteCond %{HTTP_REFERER} google\.com
RewriteCond %{QUERY_STRING} !^referrer=(.*)$ [NC]
RewriteRule ^(.*)$ $1?referrer=%{HTTP_REFERER} [L,R,QSA]
I needed to add a condition that if there is already a referrer query string parameter, don't append it. Else I was getting an infinite loop.
And I needed a external Redirect flag. Else it was not redirecting.

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]

htaccess redirect query string

How can I redirect a specific query to another URL in htaccess? The URL is:
http://miniwars.co.uk/player-search?location=&distance_max=&category%5B%5D=epic
The location, distance_max and category are always present, and I only want to redirect when location, distance_max are empty, and category is "epic".
I want to redirect to:
http://miniwars.co.uk/epic-players
The htaccess rule I'm trying is:
RewriteCond %{QUERY_STRING} ^(category=epic|location=|distance_max=)($|&)
RewriteRule ^player-search$ miniwars.co.uk/epic-players/ [L,R=301]
...But it's not doing anything. Can someone let me know where I've gone wrong?
Maybe you should try it like this.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(location=&distance_max=&category=epic)$ [NC]
RewriteRule ^player-search /epic-players/ [L,R=301]
I only want to redirect when location, distance_max are empty, and category is "epic".
You can use this rule:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)location=(&|$) [NC]
RewriteCond %{QUERY_STRING} (^|&)distance_max=(&|$) [NC]
RewriteCond %{QUERY_STRING} (^|&)category=epic(&|$) [NC]
RewriteRule ^player-search/?$ /epic-players/? [L,R=301,NC]
This will allow your query parameters in any order
? in the target URI will strip off previous query string

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>

Redirect Subdomain to new domain

Hi guys trying to get a 301 redirect working and having trouble. I need to redirect sub.domain1.com to www.domain2.com and make sure that any file names or parameters get sent over with it.
This is what I was trying:
RewriteCond %{HTTP_HOST} ^domain1.com [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [L,R=301]
I also tried this:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^sub\.domain1\.com$ /www.domain2.com? [R=301,NE,NC,L]
Where am I messing up?
You missed the subdomain part and proper escaping.
RewriteCond %{HTTP_HOST} ^sub\.domain1\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [L,R=301]
Further explain can be found in this question.
Rule of thumb for rewriterules: from the most complex to the less complex.
And don't forget the QSA directive (QSA = Query String Append = "make sure that any file names or parameters get sent over with it")
RewriteCond %{HTTP_HOST} ^sub\.domain1\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [QSA,R=301,L]
Tell me if it works.

RewriteRule to disregard a url variable

I have some pages indexed by Google, for example:
/product.html?affiliateid=142
I want a rewrite rule to 301 redirect to the same page if there's an affiliateid=xxx
So far I have this:
RewriteCond %{QUERY_STRING} ^affiliateid=[0-9]+$
RewriteRule ^$ /test.html$ [L,R=301]
But its not working, I need to get rid of the variable and get the page name somehow.
You need to specify an empty query in your substitution URL to have the original requested query not appended to the new URL:
RewriteCond %{QUERY_STRING} ^affiliateid=[0-9]+$
RewriteRule ^ %{REQUEST_URI}? [L,R=301]
And if you want to preserve any other query parameter, try this:
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)affiliateid=[0-9]+(&+(.*))?$
RewriteRule ^ %{REQUEST_URI}?%1%4 [L,R=301]
Found it:
RewriteCond %{QUERY_STRING} ^affiliateid=([0-9]+)$
RewriteRule ^(.*)$ /$1? [L,NC,R=301]
It's that question mark here /$1? that tells the rule to end the rewrite at the query string