RewriteCond www and non www to subpage - apache

I'm a newbie and the question is simple, I want mysite.eu and www.mysite.eu redirect to a subpage of their website,
but I can't get it to work for both because when I add something like this I got stuck in a loop so how can I fix this that both wil direct to that subpage without getting a loop.
My example:
RewriteCond %{HTTP_HOST} www\.mysite\.eu [NC]
RewriteRule . http://www.mysite.eu/page?page=webshop_fixol&lng=1 [L]
RewriteCond %{HTTP_HOST} mysite\.eu [NC]
RewriteRule . http://www.mysite.eu/page?page=webshop_fixol&lng=1 [R=permanent,L]

Why do you have the . there? sure you did not just want this:
RewriteCond %{HTTP_HOST} www\.mysite\.eu [NC]
RewriteRule ^/$ http://www.mysite.eu/page?page=webshop_fixol&lng=1 [L]
RewriteCond %{HTTP_HOST} mysite\.eu [NC]
RewriteRule ^/$ http://www.mysite.eu/page?page=webshop_fixol&lng=1 [R=permanent,L]

It can be done in a single rewrite rule like this:
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.eu$ [NC]
RewriteRule . page?page=webshop_fixol&lng=1 [L,QSA]
^(www\.)?mysite\.eu$ will match both sites www and non www.

Related

Stuck on RewriteRule in htaccess

I am stuck in redirecting my urls. I think i am almost there but can't oversee what i am missing. When i test the redirect is goes to the correct url, but sticks the old url behinde the new url.
This is where i got stuck:
RewriteCond %{QUERY_STRING} lang=uk&page=page
RewriteRule ^(.*)$ domain/page/ [L,R=301]
RewriteCond %{QUERY_STRING} lang=fr&page=page
RewriteRule ^(.*)$ domain/page/ [L,R=301]
RewriteCond %{QUERY_STRING} lang=uk&page=about
RewriteRule ^(.*)$ domain/about/ [L,R=301]
RewriteCond %{QUERY_STRING} lang=uk&page=about
RewriteRule ^(.*)$ domain/about/ [L,R=301]
RewriteCond %{QUERY_STRING} lang=uk&page=news
RewriteRule ^(.*)$ domain/info/ [L,R=301]
RewriteCond %{QUERY_STRING} lang=fr&page=news
RewriteRule ^(.*)$ domain/info/ [L,R=301]
I am also scared i get stuck with a big htaccess file if i do all redirects individual. Would that slow things down?
Lang=nl, uk, us, fr all go to the same page.
not all page= got the same pagename after the change.
Thanks in advance,
You can have specific redirects first where page name is not same as target followed by a single rule where query parameter page has same value as the target URI. So your code can be shortened to these 2 rules only.
RewriteEngine On
RewriteCond %{QUERY_STRING} lang=(fr|uk|us|nl)&page=news
RewriteRule ^ /info/? [L,R=301]
RewriteCond %{QUERY_STRING} lang=(fr|uk|us|nl)&page=([^&]+)
RewriteRule ^ /%2/? [L,R=301]

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]

.htaccess mod_rewrite based on domain

I have the following rule in my htaccess, I need to modify it to add variable to the end of the re-written url. Basically I have two domains pointing to the same hosting, and I am showing different sites depending on which domain it used.
RewriteCond %{HTTP_HOST} ^(www\.)?rrr\.me
RewriteCond %{REQUEST_URI} !^/rrr
Rewriterule ^(.*)$ /rrr/$1 [L]
If someone goes to rrr.me or www.rrr.me they get the index.php in the rrr directory of my website. This works great!
I now need to be able to add a variable to the index.php. So someone will type in rrr.me/abc and it will append the abc to the end of the index.php in this fashion index.php?var1=abc
NEW REWRITE RULES AFTER ADVICE FROM Olaf Dietsche IN COMMENTS.
RewriteCond %{HTTP_HOST} ^(www\.)?rrr\.me
RewriteCond %{REQUEST_URI} !^/rrr
RewriteCond %{REQUEST_URI} !/assets/
Rewriterule ^.*$ /rrr/index.php?rrr=$0 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?rrr\.me
RewriteCond %{REQUEST_URI} !^/rrr
Rewriterule ^assets/.*$ /rrr/$0 [L]
To append the requested URL as a query string
Rewriterule ^.*$ /rrr/index.php?var1=$0 [L]
To exclude the assets directory, you must use an additional RewriteCond
RewriteCond %{HTTP_HOST} ^(www\.)?rrr\.me
RewriteCond %{REQUEST_URI} !^/rrr
RewriteCond %{REQUEST_URI} !/assets/
Rewriterule ^(.*)$ /rrr/$1 [L]
For the assets files, you can use a similar redirect
RewriteCond %{HTTP_HOST} ^(www\.)?rrr\.me
RewriteCond %{REQUEST_URI} !^/rrr
Rewriterule ^assets/.*$ /rrr/$0 [L]

Prevent redirect coming from external domains

I need to prevent that an external domain make a redirect to my domain. I tried using the following rules:
RewriteCond %{HTTP_REFERER} ^http://www.extenaldomain.com [NC]
RewriteRule ^$ /my_error_page.htm [R,L]
but it doesn't work. I tried also:
RewriteCond %{HTTP_FORWARDED} ^http://www.extenaldomain.com [NC]
RewriteRule ^$ /my_error_page.htm [R,L]
but it doesn't work too. any idea?
Why don't you simply precise the domain name without adding extra stuff?
Try this, it should work:
RewriteCond %{HTTP_REFERER} extenaldomain\.com [NC]
RewriteRule (.*) /my_error_page.htm [R,L]
Tell me if it does
SOLVED: it works:
RewriteCond %{HTTP_REFERER} extenaldomain\.com [NC]
RewriteCond %{REQUEST_URI} !^/my_error_page.htm
RewriteRule (.*) /my_error_page.htm [R,L]