Exclude one url from rewrite condition - apache

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]

Related

htaccess redirect if certain query pattern found

i would like to redirect from
www.site1.info?index.php?id=243&L=&tx_kesearch_pi1%5Bsword%5D=YOU
www.site1.info/search/search/?L=&tx_kesearch_pi1[sword]=YOU
TO
www.site2.org?index.php?id=243&L=&tx_kesearch_pi1%5Bsword%5D=YOU
www.site2.org/search/search/?L=&tx_kesearch_pi1[sword]=YOU
CAUTION
i only want to redirect ONLY if ...
www.site1.info?index.php?id=243
www.site1.info/search/search/
... and than redirect with the ongoing query (porting it to the site2.org
i tried unsuccessfully:
RewriteCond %{HTTP_HOST} ^(www\.)site1\.info/index.php?id=243$ [NC]
RewriteRule ^ http://www.site2.org/index.php?id=243%{REQUEST_URI} [NE,R=301,L]
You cannot match REQUEST_URI OR QUERY_STRING in RewriteCond %{HTTP_HOST}.
You can use this code:
RewriteCond %{HTTP_HOST} ^(www\.)site1\.info$ [NC]
RewriteCond %{QUERY_STRING} (?:^|&)id=243(?:&|$) [NC]
RewriteRule ^index\.php$ http://www.site2.org/index.php?id=243%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)site1\.info$ [NC]
RewriteRule ^search/search/?$ http://www.site2.org/index.php?id=243%{REQUEST_URI} [NE,R=301,L]

combination of rewrite rules not working

RewriteEngine On
RewriteCond %{HTTP_HOST} !.com$ //redirect from .net and .org to .com
RewriteRule .* http://www.domain.com%{REQUEST_URI} [R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC] //Always use www.
RewriteRule ^/(.*)$ http://www.%1/$1 [R=301]
RewriteRule ^/?article-(.*)$ article.php?id=$1 [L,QSA,NC] // redirect article-x to article.php?id=x
RewriteRule ^/?page-(.*)$ page?p=$1 [L,QSA,NC]// redirect page-x to page.php?p=x
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L] // add the .php to every request if the file type is missing
all the rules work fine except for adding www. to url. for example http://domain.com/page-1
doesn't get redirected to http://www.domain.com/page-1
%1 refers to a pattern captured with () in the preceding RewriteCond. You haven't captured any patterns in RewriteCond. Instead, you can just use %{HTTP_HOST} in the RewriteRule.
If these rules reside in .htaccess, RewriteRule will not contain a leading /, and will therefore never match if your pattern does. Remove the / and match on ^(.*)$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301]
Side note: don't forget to escape the . in .com. It will work unescaped, but the expression doesn't mean what you literally intend it to mean.
RewriteCond %{HTTP_HOST} !\.com$
The full set of rewrites all together should look like the following (I have removed all the leading / and /? expressions). I have tested all of this as working on my own system.
RewriteEngine On
RewriteCond %{HTTP_HOST} !\.com$ [NC]
#Use a capture group here, add [L]
RewriteRule (.*) http://www.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteRule ^article-(.*)$ article.php?id=$1 [L,QSA,NC]
RewriteRule ^page-(.*)$ page?p=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

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]

Apache .hatccess rewrites not working for legacy URLS

I'm trying to rewrite some legacy Joomla URLs on a site that's now using ExpressionEngine as its CMS but they're not working.
The ExpressionEngine URL rewrites, i.e. removing index.php from the URL work fine though.
This is what I've got:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
# This is the one that's not working
RewriteRule /index.php?option=com_chronocontact&Itemid=54 /contact/ [R=301,L]
# Force www
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
# Redirect index.php Requests
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php [NC]
RewriteCond %{THE_REQUEST} ^GET
RewriteRule ^index\.php(.+) $1 [R=301,L]
# Standard ExpressionEngine Rewrite
RewriteCond %{REQUEST_URI} ^/
RewriteCond %{QUERY_STRING} ^(gclid=.*)
RewriteRule ^(.+) /index.php?/ [L,PT]
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(assets|css|images|tinymce|js|min|cms|themes|index\.php|admin\.php|favicon\.ico|index\.php|path\.php|php\.ini) [NC]
RewriteRule ^(.+) /index.php?/ [L]
</IfModule>
Can anyone spot what I'm doing wrong?
The first thing is the stray RewriteCond %{HTTPS} !=on that you have at the top. It looks like it belongs to the rule under it, as in:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
As far as the rule that you have commented that doesn't work, the ? is a reserved character for regular expressions, and your pattern actually says that the second p in /index.php is "optional". Additionally, you can't match against the query string in a rewrite rule, you need to use a rewrite condition and match against the %{QUERY_STRING} variable:
RewriteCond %{QUERY_STRING} ^option=com_chronocontact&Itemid=54$
RewriteRule ^(index.php)?$ /contact/? [R=301,L]
is probably more along the lines of what you're looking for.

redirecting file request for subdomain and main site with certain arguments?

I am running wildcard subdomains. the aim of the site is to run a virtual subdomain. what the functionality should be is that when the files
from the main site are called they will be rewritten for the clean URLs. But when i am calling the same files form the subdomain
like xyz.doamin.com/my-file.php then this should be rewritten like it is calling that file with an argument of subdoamin like
domain.com/my-file.php?var=xyz. Currently what i have done for the file call is this
Options +FollowSymLinks
RewriteEngine on
## Redirecting all non-www domains to www.domain.com
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,NC,L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ http://www.domain.com%{REQUEST_URI}?var=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ http://www.domain.com%{REQUEST_URI}?%{QUERY_STRING}&var=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ http://www.domain.com/index.php?subdomain=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ http://www.domain.com%{REQUEST_URI}?%{QUERY_STRING}&subdomain=%1 [L]
This is throwing a 500 internal error for the file call.
Rule set 4 will overlap with rule set 1 and 2. I suspect this is causing the issue.
So I think all 3 need an extra RewriteCond check on REQUEST_URI.
Really you shouldn't need rule set 4. Just including index.php as a default should do the trick. Unless of course you don't want URL's like http://www.domain.com/?var=xyz, in this case you need rule 4, but you need to split it into 2 - one with a QUERY_STRING and one without, just like rule 1 and 2
Also, isn't %1 from the regex in the rewriteRule itself and not the preceeding rewriteConds? If so then your regex is wrong, because it'll be setting the var= to the entire original URL.
Looking in the error log is a good idea as it will give you the reason for the 500 error.
Edit: Ok, try something like this (not tested)
## Redirecting all non-www domains to www.domain.com
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L,QSA]
## Redirecting / requests to index.php
# Note: Use the QSA flag for handling with or without query string
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/index.php?var=%1 [L,QSA]
## Redirecting /something requests to index.php
# Note: Use the QSA flag for handling with or without query string
RewriteCond %{REQUEST_URI} ^/.+$
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com%{REQUEST_URI}?var=%1 [L,QSA]