Conditional "OR" operation in RewriteRule - apache

I want to redirect certain urls of my old website to the new website. This redirection to new website is conditional i.e if query param contains key "site" with value as "eu", "jp" or "in" then only redirect otherwise do not.
RewriteCond %{QUERY_STRING} (?:^|&)site=(eu) [NC,OR]
RewriteCond %{QUERY_STRING} (?:^|&)site=(in) [NC,OR]
RewriteCond %{QUERY_STRING} (?:^|&)site=(jp) [NC]
RewriteRule ^/?fetchHomePage.action$ https://example.com/%1? [R=301,L,NC]
RewriteRule ^/?fetchFirstPage.action$ https://example.com/firstpage/%1? [R=301,L,NC]
RewriteRule ^/?fetchSecondPage.action$ https://example.com/fetchsecondpage/%1? [R=301,L,NC]
RewriteRule ^/?fetchThirdPage.action$ https://example.com/fetchThirdPage/%1? [R=301,L,NC]
With above configuration it works well for path "/fetchHomePage" but for other paths in the URLs ("/fetchFirstPage", "/fetchSecondPage" and "/fetchThirdPage") browser is always redirecting to the new website irrespective of what the site param is there in the query parameter.

You may follow one of these method :
Here you need to add the condition for all the rules.
RewriteCond %{QUERY_STRING} (?:^|&)site=(eu|in|jp)(?:&|$) [NC]
RewriteRule ^/?fetchHomePage.action$ https://example.com/%1? [R=301,L,NC]
RewriteCond %{QUERY_STRING} (?:^|&)site=(eu|in|jp)(?:&|$) [NC]
RewriteRule ^/?fetchFirstPage.action$ https://example.com/firstpage/%1? [R=301,L,NC]
RewriteCond %{QUERY_STRING} (?:^|&)site=(eu|in|jp)(?:&|$) [NC]
RewriteRule ^/?fetchSecondPage.action$ https://example.com/fetchsecondpage/%1? [R=301,L,NC]
RewriteCond %{QUERY_STRING} (?:^|&)site=(eu|in|jp)(?:&|$) [NC]
RewriteRule ^/?fetchThirdPage.action$ https://example.com/fetchThirdPage/%1? [R=301,L,NC]
OR
You can also write multiple rules but for that you need to remove the L flag from all the rules except last rule.
RewriteCond %{QUERY_STRING} (?:^|&)site=(eu|in|jp)(?:&|$) [NC]
RewriteRule ^/?fetchHomePage.action$ https://example.com/%1? [R=301,NC]
RewriteRule ^/?fetchFirstPage.action$ https://example.com/firstpage/%1? [R=301,NC]
RewriteRule ^/?fetchSecondPage.action$ https://example.com/fetchsecondpage/%1? [R=301,NC]
RewriteRule ^/?fetchThirdPage.action$ https://example.com/fetchThirdPage/%1? [R=301,L,NC]
L = Last
The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed. This corresponds to the last command in Perl, or the break command in C. Use this flag to indicate that the current rule should be applied immediately without considering further rules.
Reference : https://httpd.apache.org/docs/2.4/rewrite/flags.html

Related

.htaccess external & internal rewrite conflict

I'm trying to rewrite a query string to a path, like so:
http://example.com/?p=page1
to
http://example.com/page/page1
The internal redirect works and I can view the page at the second URL but as soon as I try to redirect the first URL to the second, I get a 'Too many redirects' error.
.htaccess:
RewriteCond %{REQUEST_URI} /page/(.*)
RewriteRule page/(.*)$ index.php?p=$1 [L,QSA,NC]
RewriteCond %{QUERY_STRING} ^p=(.*)$ [NC]
RewriteRule (.*) /page/%1? [R=301,L,NE,QSD,NC]
The first two lines are working by themeselves. The addition of the last two lines causes the error.
Your rule is redirecting the uri back it itself that is why you got the redirect error.
You can use %{THE_REQUEST} or %{ENV_REDIRECT_STATUS} variables to avoid Too many redirect error .
RewriteCond %{REQUEST_URI} /page/(.*)
RewriteRule page/(.*)$ index.php?p=$1 [L,QSA,NC]
RewriteCond %{ENV_REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^p=(.*)$ [NC]
RewriteRule (.*) /page/%1? [R=301,L,NE,QSD,NC]
or
RewriteCond %{REQUEST_URI} /page/(.*)
RewriteRule page/(.*)$ index.php?p=$1 [L,QSA,NC]
RewriteCond %{THE_REQUEST} /\?p=.+ [NC]
RewriteCond %{QUERY_STRING} ^p=(.*)$ [NC]
RewriteRule (.*) /page/%1? [R=301,L,NE]

htaccess replace characters in query parameter

I have a url like http://www.example.com/?product=test&retailer=test&site=test
In this instance (where product parameter is present) I remove &site=test but leave the rest of the url untouched.
If product parameter isn't present, e.g. http://www.example.com/?retailer=test&site=test
I remove &site=test and change ?retailer=test to /retailer/test so the full url would be http://www.example.com/retailer/test. I also only make this happen on the root domain.
I do this using these rules
# first condition
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{QUERY_STRING} ^product=([^&\s]+)&retailer=([^&\s]+)&site=test$ [NC]
RewriteRule ^$ /?product=%1&retailer=%2 [R=301,L]
# second condition
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{QUERY_STRING} ^retailer=([^&\s]+)&site=test$ [NC]
RewriteRule ^$ /retailer/%1? [R=301,L]
on the second rule when the url is rewritten to /reatiler/test there is the possibility for this to be /retailer/test+test in this instance I need to change it to /retailer/test-test could also be /retailer/test+test+test which would need to be /retailer/test-test-test
help on this would be much appreciated
You could use the N flag (more info here):
RewriteRule ^retailer/(.*)\+(.*)$ /retailer/$1-$2 [N,R=301]
Note that this is actually equivalent, since a redirect is involved here:
RewriteRule ^retailer/(.*)\+(.*)$ /retailer/$1-$2 [R=301,L]
So finally your htaccess should look like this
# first condition
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{QUERY_STRING} ^product=([^&\s]+)&retailer=([^&\s]+)&site=test$ [NC]
RewriteRule ^$ /?product=%1&retailer=%2 [R=301,L]
# second condition
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{QUERY_STRING} ^retailer=([^&\s]+)&site=test$ [NC]
RewriteRule ^$ /retailer/%1? [R=301,L]
RewriteRule ^retailer/(.*)\+(.*)$ /retailer/$1-$2 [R=301,L]

Exclude one url from rewrite condition

I have following mod_rewrite rule to redirect from site.com to www.site.com:
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=permanent,L] .
I need to exclude from this rule urls starting with /yandex_market
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{REQUEST_URI} !^/yandex_market.*$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=permanent,L] .
But rule still works on urls starting with /yandex_market How to fix it?
Problem is that your 2nd rule rewrites /yandex_market/foo URI to /index.php?module=YandexPurchaseView&type=foo and thus making RewriteCond %{REQUEST_URI} !^/yandex_market.*$ [NC] condition succeed. You will need to use %{THE_REQUEST} variable for your condition which doesn't change with application of rewrite rules.
Keep your rules like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{THE_REQUEST} !/yandex_market [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,NE,L]
RewriteRule ^yandex_market/(.+)$ index.php?module=YandexPurchaseView&type=$1 [L,NC,QSA]

Second Rewriterule not working correctly

I am trying to do two things here..
redirect http://site.com.au/brand.php?pBrand=THENORTHFACE to http://site.com.au/brand/the-north-face (this works fine)
redirect http://site.com.au/listingbrand.php?pBrand=THENORTHFACE to http://site.com.au/brand/the-north-face (this does not work, when redirecting listingbrand.php?pBrand=DOSH or pBrand=ATKM, they both point back to the first rewrite the-north-face).
How do i make the second rewrite work for each brand? Also, is it correct to repeat the rewrites for each brand?
RewriteCond %{QUERY_STRING} ^pBrand=THENORTHFACE$ [NC]
RewriteRule ^brand\.php$ /brand/the-north-face/? [R=301,L]
RewriteRule ^listingbrand\.php$ /brand/the-north-face/ [R=301,L]
RewriteCond %{QUERY_STRING} ^pBrand=DOSH$ [NC]
RewriteRule ^brand\.php$ /brand/dosh/? [R=301,L]
RewriteRule ^listingbrand\.php$ /brand/dosh/ [R=301,L]
RewriteCond %{QUERY_STRING} ^pBrand=ATKM$ [NC]
RewriteRule ^brand\.php$ /brand/all-the-kings-men/? [R=301,L]
RewriteRule ^listingbrand\.php$ /brand/all-the-kings-men/ [R=301,L]
You have this rule 3 times:
RewriteRule ^listingbrand\.php$ ...
Which is not using RewriteCond since RewriteCond is only applicable to very next RewriteRule. Actually you don't even need a separate rule since earlier RewriteRule can handle both brand.php and listingbrand.php using OR in regex.
Change your code to this:
RewriteCond %{QUERY_STRING} ^pBrand=THENORTHFACE$ [NC]
RewriteRule ^(brand|listingbrand)\.php$ /brand/the-north-face/? [R=301,L,NC]
RewriteCond %{QUERY_STRING} ^pBrand=DOSH$ [NC]
RewriteRule ^(brand|listingbrand)\.php$ /brand/dosh/? [R=301,L,NC]
RewriteCond %{QUERY_STRING} ^pBrand=ATKM$ [NC]
RewriteRule ^(brand|listingbrand)\.php$ /brand/all-the-kings-men/? [R=301,L,NC]

mod_rewrite rules, how to test for 'not a string'?

I have a blog installed in www.foo.com/wp/ and would like all requests that go to any page that doesn't start with /wp/ to be redirected to www.foo.com/wp/ (without preservation of the URL).
E.g.:
www.foo.com/bar/baz/hello.gif > www.foo.com/wp/
Also, at the same time, I need the URL to have www. added if it doesn't (which might be where my rules are getting confused)
At the moment, I have:
RewriteCond %{HTTP_HOST} !^www.foo.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.foo.com/$1 [R=permanent]
RewriteRule ^(?!wp)/(.*)$ http://%{HTTP_HOST}/wp/($2) [R=permanent]
But I think this is currently completely broken.
Help much appreciated.
RewriteCond %{HTTP_HOST} !^www.foo.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule (.*) http://www.foo.com/$1 [R=permanent,L]
RewriteRule ^(?!wp/)(.*) http://%{HTTP_HOST}/wp/$1 [R=permanent]
The path portion of the URL being matched never has a leading slash, and you don't need to anchor a .*.
L modified prevents rewriting from continuing after you arrive at your first redirect.
Negative-lookbehind (?!) is not a capturing construct, and you substitute in captured patterns by writing $1, not ($1).
Edit: Apparently for OP the last rule doesn't work, failing to exclude the cases that begin with /wp/, which makes no sense to me but whatever. Here's an attempt at a workaround:
RewriteCond %{HTTP_HOST} !^www.foo.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule (.*) http://www.foo.com/$1 [R=permanent,L]
RewriteCond %{REQUEST_URI} !^/wp/
RewriteRule (.*) http://%{HTTP_HOST}/wp/$1 [R=permanent]
Try these rules:
RewriteCond %{HTTP_HOST} !^www.exmaple.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301,L]
RewriteCond %{THE_REQUEST} ^GET\ /wp/
RewriteRule ^wp/(.*) /$1 [R=301,L]
RewriteRule !^wp/ wp%{REQUEST_URI} [L]
The first rule is for the host name. The second is to remove the /wp path prefix externally. And the third is to add the prefix again for internal redirection.