QUERY_STRING Rewrite order - mod-rewrite

I have the following rewrite rules which are rewriting to the same location:
http://test.com/finance/payroll/forms/salary-payment-form.shtml?staff=test-edu-trust -> TO ->https://www.test.com/finance/BBBB.shtml?staff=test-edu-trust
RewriteRule %{QUERY_STRING} ^staff=test-edu-trust$
RewriteRule ^/finance/payroll/forms/salary-payment-form.shtml$ https://www.test.com/finance/BBBB.shtml?staff=test-edu-trust[R=301,L,NC]
http://test.com/finance/payroll/forms/wages-payment-form.shtml?staff=test-comm-rac -> TO -> https://www.test.com/finance/BBBB.shtml?staff=test-comm-rac
RewriteRule %{QUERY_STRING} ^staff=test-comm-rac$
RewriteRule ^/finance/payroll/forms/salary-payment-form.shtml$ https://www.test.com/finance/BBBB.shtml?staff=test-comm-rac[R=301,L,NC]
The problem is both rewrite to the same location - https://www.test.com/finance/BBBB.shtml?staff=test-edu-trust
If I change the order of the rules to below then they end up rewriting to the first location in order: https://www.test.com/finance/BBBB.shtml?staff=test-comm-rac as per below:
RewriteRule %{QUERY_STRING} ^staff=test-comm-rac$
RewriteRule ^/finance/payroll/forms/salary-payment-form.shtml$ https://www.test.com/finance/BBBB.shtml?staff=test-comm-rac[R=301,L,NC]
RewriteRule %{QUERY_STRING} ^staff=test-edu-trust$
RewriteRule ^/finance/payroll/forms/salary-payment-form.shtml$ https://www.test.com/finance/BBBB.shtml?staff=test-edu-trust[R=301,L,NC]
What is causing the order of the rules here just to rewrite to the first one defined as I thought the QUERY_STRING would have caught this and sorted which url to rewrite to ??

Just noticed i have RewriteRule and not RewriteCond in for the QUERY_STRING !! Doh !!!

Related

Basic rewrite of url

I need a rule that rewrites
http://example.com/page.html?page=2
to
http://example.com/page.html/page/2
I already tried this rule:
RewriteRule ^(.*)page=(.*)$ http://example.com/$1/page/$2/
but this does not work! :( Where I'm wrong?
You can try this one:
RewriteCond %{QUERY_STRING} page=(\d+) [NC]
RewriteRule (.*) /$1/page/%1/? [R=301,L]
It should works.

Rewriting URLs with one query string to another URL with a different query string

I'm trying to accomplish some redirects as follows:
#Basic Rule Set
RewriteRule ^/path$ http://newhost.com/Page?pagename=form1000 [R=301]
RewriteRule ^/path/index.html$ http://newhost.com/Page?pagename=form1000 [R=301]
which work fine.
However, redirects with query strings in source and destination return me to the above destination url. For e.g.,
# Advanced Rule Set
RewriteRule ^/path/index.html?var=foo$ http://newhost.com/Page?pagename=form1000?id=hello [R=301]
RewriteRule ^/path/index.html?var=bar$ http://newhost.com/Page?pagename=form1000?id=byebye [R=301]
RewriteRule ^/path/index.html?var=xyz$ http://newhost.com/Page?pagename=form1000?id=world [R=301]
all redirect to http://newhost.com/Page?pagename=form1000.
I've tried a RewriteCond %{QUERY_STRING} ^var=(.*)$ [NC] and RewriteCond %{REQUEST_URI} ^/path/index.html [NC] preceding the above three rules, and still I'm redirected to http://newhost.com/Page?pagename=form1000. I've swaped the order of the Basic and Advanced rule sets, and all redirect to http://newhost.com/Page?pagename=form1000.
Any ideas on how I might get these rule sets to work? CentOS 6.x, Apache 2.2.
You may try this instead:
# Advanced Rule Set
RewriteCond %{QUERY_STRING} var=foo [NC]
RewriteRule ^path/index.html/? http://newhost.com/Page?pagename=form1000?id=hello [R=301,L,NC]
RewriteCond %{QUERY_STRING} var=bar [NC]
RewriteRule ^path/index.html/? http://newhost.com/Page?pagename=form1000?id=byebye [R=301,L,NC]
RewriteCond %{QUERY_STRING} var=xyz [NC]
RewriteRule ^path/index.html/? http://newhost.com/Page?pagename=form1000?id=world [R=301,L,NC]

mod_rewrite to redirect URL with query string

I have a lot of working mod_rewrite rules already in my httpd.conf file. I just recently found that Google had indexed one of my non-rewritten URLs with a query string in it:
http://example.com/?state=arizona
I would like to use mod_rewrite to do a 301 redirect to this URL:
http://example.com/arizona
The issue is that later on in my rewrite rules, that 2nd URL is being rewritten to pass query variables on to WordPress. It ends up getting rewritten to:
http://example.com/index.php?state=arizona
Which is the proper functionality. Everything I have tried so far has either not worked at all or put me in an endless rewrite loop. This is what I have right now, which is getting stuck in a loop:
RewriteCond %{QUERY_STRING} state=arizona [NC]
RewriteRule .* http://example.com/arizona [R=301,L]
#older rewrite rule that passes query string based on URL:
RewriteRule ^([A-Za-z-]+)$ index.php?state=$1 [L]
which gives me an endless rewrite loop and takes me to this URL:
http://example.com/arizona?state=arizona
I then tried this:
RewriteRule .* http://example.com/arizona? [R=301,L]
which got rid of the query string in the URL, but still creates a loop.
Ok, adding the 2nd RewriteCond finally fixed it - now rewriting correctly and no loop:
# redirect dynamic URL: ?state=arizona
RewriteCond %{QUERY_STRING} state=arizona [NC]
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteRule .* http://domain.com/arizona? [R=301,L]
# older rewrite rule that passes query string based on URL:
RewriteRule ^([A-Za-z-]+)$ index.php?state=$1 [L]
And here's the code to make it work for any state value, not just arizona:
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteCond %{QUERY_STRING} ^state=([A-Za-z-]+)$ [NC]
RewriteRule .* http://domain.com/%1? [R=301,L]
RewriteCond %{REQUEST_URI} ^/\?state=arizona$ [NC]
should be
RewriteCond %{QUERY_STRING} state=arizona [NC]
The request_uri ends at the ? token.

Very simple : Mod_rewrite

I have the following rewrite rule in place:
RewriteRule ^register/?$ /fitch/index.php?cmd=register [NC,L,QSA]
Requests to mysite.com/register are rewritten to
mysite.com/fitch/index.php?cmd=register
I want to allow an optional parameter to be passed as well, such that requests to
mysite.com/register/sender=models directs me to
mysite.com/fitch/index.php?cmd=register&sender=models
Perhaps:
RewriteRule ^register/(.*) /fitch/index.php?cmd=register&$1
For any number of paths -> query string params:
RewriteCond %{REQUEST_URI} ^/register/
RewriteRule ^(.*)/(.*)$ /$1&$2 [L]
RewriteCond %{REQUEST_URI} ^/register&
RewriteRule ^(.*)$ /fitch/index.php?cmd=$1 [L,QSA]
This will rewrite URL's like:
http://mysite.com/register/a=b/1=2/c=d
to:
http://mysite.com/fitch/index.php?cmd=register&a=b&1=2&c=d

How mod_rewrite can add subdomain?

RewriteEngine On
RewriteRule ^/ai?$ /Market/publish.jsp [QSA,L,PT]
RewriteRule ^/ar?$ /Market/MailDispatch [QSA,L,PT]
RewriteCond %{HTTP_HOST} !^web\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://web.example.com/$1 [L,R]
#How skip www\. to web\. for this 1 ?
#RewriteRule ^/vi/?([0-9]+)\.htm$ /Market/vi.do?id=$1 [PT,L]
RewriteRule ^/li /Market/list.do [QSA,PT,L]
RewriteRule ^/vi/locations.jsp /Market/locations.jsp [PT,L]
ErrorDocument 404 /notfound.html
Nearly undoable(?) I try http://example.com/vi/{N}.htm should redirect to http://web.example.com/vi/{N}.htm where N is dynamic ID.
Seen mod_rewrite with subdomain and url pattern
There is no clear way to make eg http://example.com/vi/1096.htm pass up to next version http://web.example.com/vi/1096.htm where number is dynamic. I tried
A rule with the following scheme should do it:
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^/vi/\d+\.htm$ http://web.example.com%{REQUEST_URI} [L,R=301]
It’s important to put this rule in front of those rules that do an internal redirect. Otherwise an already internally rewritten URL could be rewritten externally.
If you want to use this rule in a .htaccess file, remove the leading slash from the pattern in RewriteRule.

Resources