How to redirect a URL with a querystring - apache

I've moved a website to a new domain and I've used RewriteRule to redirect a number of my URLs.
I'm stuck trying to redirect URLs with query strings in them?
RewriteRule /news/archive.php?cat=economic-impact http://www.new-website.com/faqs/economic-impact [R=301,L]
RewriteRule /news/archive.php?cat=housing-need http://www.new-website.com/faqs/housing-need R=301,L]
RewriteRule /news/archive.php?cat=infrastructure http://www.new-website.com/faqs/infrastructure R=301,L]
Further up the htaccess file I'm using this to redirect the domain
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old-website.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.old-website.com$
RewriteRule (.*)$ http://www.new-website.com/$1 [R=301]
When I visit:
old-website.com/news/archive.php?cat=economic-impact
it redirects to
new-website.com/news/archive.php?cat=economic-impact
Which 404s, I need to to redirect to:
new-website.com/faqs/economic-impact
Can anyone help?

You need to add a blank query string on your rewrite to truncate any existing query strings.
The below should do the job:
RewriteRule (.*)$ http://www.new-website.com/$1? [R=301]
In addition, the way you're checking the existing query strings won't work. You'll need to check them separately:
RewriteCond %{QUERY_STRING} "cat=(economic-impact|housing-need|infrasructure)"
RewriteRule news/archive.php http://www.new-website.com/faqs/%1? R=301,L]
The %1 will take a backreference from a regex group match in the RewriteCond

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.

Redirection is not working with mod_rewrite in htaccess

I need to redirect few URIs having query string like:
/pages/foo.bar?pageId=123456 to http://some.site/spam/egg/
/pages/foo.bar?pageId=45678 to http://another.site/spaming/egging/
I have this in my htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/pages/foo.bar$
RewriteCond %{QUERY_STRING} ^pageId=123456$
RewriteRule ^.*$ http://some.site/spam/egg/ [R=301,L]
RewriteCond %{REQUEST_URI} ^/pages/foo.bar$
RewriteCond %{QUERY_STRING} ^pageId=45678$
RewriteRule ^.*$ http://another.site/spaming/egging/ [R=301,L]
But its not working, showing 404. What am i doing wrong?
You need to move these 2 rules i.e. before all other rules just below RewriteEngine On line as other rules might be overriding this.
(Based on your comments) Your culprit rule is this rule:
RewriteRule . index.php [L]
Which is actually rewriting every request to index.php and changing value of REQUEST_URI variable to /index.php thus causing this condition to fail:
RewriteCond %{REQUEST_URI} ^/pages/foo.bar$
From your example, you get redirected to
http://some.site/spam/egg/?pageId=123456
http://another.site/spaming/egging/?pageId=45678
You can use your browser developer tools to see the redirection (in the Network tab).
Maybe the query strings in the redirected URL lead to a 404? You can add a ? at the end of your redirection to clear the query string:
RewriteCond %{REQUEST_URI} ^/pages/foo.bar$
RewriteCond %{QUERY_STRING} ^pageId=45678$
RewriteRule ^.*$ http://another.site/spaming/egging/? [R=301,L]

Create RewriteRule Split query string in .htaccess for _escaped_fragment_

I am redirecting requests to snapshots directory for google hashbang.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?(.*)$
RewriteRule ^(.*)$ /snapshots/%1? [NC,L]
So: http://site/?_escaped_fragment_=/detail/10
Go here: site/snapshots/detail/10
But I want to ignore the second params so
http://site/?_escaped_fragment_=/detail/10/12-12-2014
Should be redirect to the same url
site/snapshots/detail/10
Any idea how I should rewrite the rules?
Try changing this line:
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?(.*)$
to
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?([^/]+(?:/[^/]+|))
So the regex will only attempt to capture as many as 2 virtual paths.

Simple Apache mod_rewrite remapping

I have to remap a few ID's to URL strings (301 redirect) and I have to do it with mod_rewrite:
/page.php?id=15 to /pagexy
/page.php?id=10 to /pageyz
The rule:
RewriteRule ^page.php?id=15$ /pagexy [L,R=301]
doesn't work. What am I doing wrong?
You need to inspect the query string separately. The following should work:
RewriteEngine On
RewriteCond %{QUERY_STRING} id=15\b [NC]
RewriteRule ^/page.php$ /pagexy? [L,R=301]
RewriteCond %{QUERY_STRING} id=10\b [NC]
RewriteRule ^/page.php$ /pageyz? [L,R=301]
Given you are tying ids to pages, it may also pay to look at using a RewriteMap

.htaccess rewrite rule causes endless loop

I want my .htaccess file to redirect to some page if any wildcard as a subdomain entry hit the browser. i.e. I want
sam.xyz.com
To redirect to
sam.xyz.com/view.php?id=sam
I am using following rewrite rules for redirect.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.xyz.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+).xyz.com
RewriteRule ^(.*)$ /view.php?id=%1 [L,R]
Problem i am facing is that it does not shift to new domain keeping query string instead it generates an endless loop
sam.xyz.com
redirects to
http://sam.xyz.com/view.php?id=sam
But doesnt move to url above without endless loop.
Kindly help me out.
Thanks in advance,
you should add a condition for redirect to prevent redirection loop:
RewriteCond %{REQUEST_URI} !^/view\.php
the whole code would be:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.xyz.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+).xyz.com
RewriteCond %{REQUEST_URI} !^/view\.php
RewriteRule ^(.*)$ /view.php?id=%1 [L,R]
You are redirecting to: prefix.domain.tld/view.php?id=prefix
Ensure that the url does not contain: id=prefix.
This solution prevents, that someone call's the url: aaa.example.com/view.php?id=bbb
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.xyz.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+).xyz.com
RewriteCond %1::%{QUERY_STRING} !^([^:]+)::.*id=\1
RewriteRule ^ /view.php?id=%1 [L,R]
Note: (.*) in the rewrite rule is obsolete.
Leave the R away to do not redirect the visitor to the url (/view.php?id=%1)