using multiple rewrites in .htaccess to remove duplicate content - .htaccess

So basically I want everyone who asks example.com/index.php to get 301 redirected to example.com/,
everyone who asks for any www. urls to get 301 redirected to corresponding non-www site
(www.examle.com/foo -> example.com/foo)
and everyone who asks for site with double [also triple etc.] slashes in url to get 301
redirected to url with double slashes remowed (example.com////foo -> example.com).
And if anyone should do any combination of these three cases, he should still get 301 redirected to right url (www.example.com////index.php -> example.com).
So I come up with that:
RewriteCond %{HTTP_HOST} !^example.com$
RewriteRule ^(.*)$ http://example.com/$1 [R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ / [R=301,N]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/{2,} [NC]
RewriteRule ^(.*)$ /$1 [R=301]
BUT, www.example.com//foo -> http://example.com/http:/example.com/foo = 404!
How to get it working?

Try these lines :
RewriteCond %{HTTP_HOST} !^example.com$
RewriteRule ^(.*)$ http://example.com/$1 [L,QSA,R=301]
RewriteRule ^index.php$ / [L,QSA,R=301]
RewriteRule ^(.*)/{2,}(.*)$ /$1/$2 [L,N,QSA,R=301]

Screw logic, seriously...
I found out, that folloving stuff in .htaccess does proper (almoust, with little quirks) 301 redirects to right places...
RewriteBase /
RewriteEngine on
...at the top, and the rewrite rules themselves:
RewriteCond %{HTTP_HOST} !^example.com$
RewriteRule ^(.*)$ http://example.com/$1 [L,QSA,R=301]
RewriteRule ^index\.php$ / [QSA,R=301]
I swear that these are only rewrite rules in my .htaccess.
The quirk is, that www.example.com//foo redirects(301) to example.com/foo, and then example.com/foo redirects(301) again to example.com/foo, bot only once... (no loops)
www.example.com//foo -> example.com/foo -> example.com/foo
Also when www and any other condition is met, redirection happens in steps:
www.example.com/index.php -> example.com/index.php -> example.com/
www.example.com//index.php -> example.com/index.php -> example.com/
But that's not really big problem...

Related

Redirect after a RedirectRule has taken place to other website

I have a website, let's say www.example.com but this used to be www.example.nl. All traffic from www.example.nl is now redirected to www.example.com, but as we changed some naming conventions, www.example.nl/seeds now has to redirect to www.example.com/nl/flower-seeds.
The .htaccess file I got contains the following code:
RewriteEngine On
RewriteBase
RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L, R=301]
Redirect 301 /seeds/(.*) example.com/nl/flower-seeds/$1
When I navigate to www.example.nl/seeds/ I end up at www.example.com/seeds/ which ends up in a 404 because I'm missing the /nl/flower- part.
I think Redirect doesn't work properly when the URL is already altered by a RewriteRule. How would you tackle this problem? Any help is appreciated!
You should exclude /seeds/ directory form general rules like this :
RewriteEngine On
RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteCond %{REQUEST_URI} !/seeds/(.*)
RewriteRule ^(.*)$ http://www.example.com/$1 [L, R=301]
RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteRule ^seeds/(.*)$ http://www.example.com/nl/flower-seeds/$1 [L, R=301]
Note: clear browser cache then test
Also , don't use regex with redirect like what you did :
Redirect 301 /seeds/(.*) example.com/nl/flower-seeds/$1
Here this (.*) has no meaning , you could use either RedirectMatch or RewriteRule

Htaccess redirect subdomain doesn't work

I have a subdomain called es and I need when someone wants to enter mysite.com/es it can be redirect to es.mysite.com. It works with the following htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mysite.com$ [NC]
RewriteRule ^(.*)$ http://es.mysite.com/$1 [R=301,L]
RedirectMatch permanent ^/es/?$ http://es.mysite.com/$1
The problem is when someone types mysite.com/es/bla/bla/bla. In this case, with the current configuration on my htaccess, the user isn't redirected and I want the user can be redirected.
For example:
If I enter:
http://letsbonus.com/es/barcelona/spa-experiencie-para-2-opcion-masaje-desconecta-roc-nature-273710
This is redirect to:
http://es.letsbonus.com/barcelona/spa-experiencie-para-2-opcion-masaje-desconecta-roc-nature-273710
Thanks in advance.
You need just this one rule in your root .htaccess of mysite.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?(mysite\.com)$ [NC]
RewriteRule ^es/(.*)$ http://es.%1/$1 [R=301,L,NE]
Your two rules enter into conflict:
Base url: http://example.com/es/test
Non-www redirection -> http://www.example.com/es/test
es subdomain redirection -> http://es.example.com/test
Non-www redirection... (we're no longer under www subdomain)
I would use this htaccess to get the excepted result:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|es).example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteRule ^es/(.*)$ http://es.example.com/$1 [R=301,L]

.htaccess combined rules (redirect non-ssl to ssl AND exections)

Basically I have 2 different rules/tasks which should be combined in one working set of rule.
Task 1
All non-ssl pages have to be redirected to ssl. My solution is already working on its own.
Dummy example:
non-ssl://sample.some-website.ch/de/test.html -> ssl://sample.some-website.ch/de/test.html
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^sample.some-website.ch$
RewriteRule ^(.*)$ "https\:\/\/sample\.some\-website\.ch\/$1" [R=301,L]
Task 2
All old .phtml Links have to be redirected to new SSL pages (individually). My solution is working on its own (2 dummy examples)
non-ssl://sample.some-website.ch/xyz.phtml -> ssl://sample.some-website.ch/de/newlink.html
non-ssl://sample.some-website.ch/123.phtml -> ssl://sample.some-website.ch/en/something.html
redirect 301 /xyz.phtml https://sample.some-website/de/newlink.html
redirect 301 /123.phtml https://sample.some-website.ch/en/something.html
Problem
I need a .htaccess solution which combines Task 1 and Task 2.
Certain old .phtml (non ssl) links have to redirected to new specific SSL-Pages AND all 'other normal' non-ssl links have to be automatically redirected to its corresponsing SSL-pages.
In the end only ssl-pages are displayed.
Any ideas?
Thanks. Hans.
You can have your .htaccess like this:
RewriteEngine On
RewriteBase /
# redirect old links
RewriteRule ^xyz\.phtml$ https://sample.some-website/de/newlink.html [L,R=301]
RewriteRule ^123\.phtml$ https://sample.some-website.ch/en/something.html [L,R=301]
# add https to your site
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# route .html file to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*\.html$ index.php [L]
# earlier rules to rewrite to index.php
RewriteRule ^[a-z]{2}(-[A-Z]{2})?/$ index.php [L]
RewriteRule ^([a-z]{2}(-[A-Z]{2})?)$ $1/ [R=301,L]

htaccess redirection subdomain

I want to redirect feedback.domain.de to www.domain.de/de/abc/cde.html via htaccess.
My current htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.de$
RewriteRule ^(.*)$ http://www.domain.de/$1 [L,R=301]
SO, I thought just to add:
redirect 301 feedback.domain.de www.domain.de/de/abc/cde.html
but it doesn't work. If i try to open feedback.domain.de it redirects to www.domain.de
I know this is a very easy question but I don't how solve it in htaccess :-(
The result I want is:
domain.de -> www.domain.de/de/index.html
www.domain.de -> www.domain.de/de/index.html
domain.de/de/example.html -> www.domain.de/de/example.html
etc...
feedback.domain.de -> www.domain.de/de/feed.html
Best regards
The Redirect directive only accept a path relative to the root path (for example /my-path) and won't match on the host part of the URL.
Try this (note that here I assume you want to redirect domain.de to www.domain.de and not all subdomains):
RewriteEngine On
# Redirect feedback.domain.de to http://www.domain.de/de/abc/cde.html
RewriteCond %{HTTP_HOST} ^feedback\.domain\.de$
RewriteRule . http://www.domain.de/de/abc/cde.html [L,R=301]
# Redirect domain.de to www.domain.de
RewriteCond %{HTTP_HOST} ^domain\.de$
RewriteRule . http://www.domain.de/$0 [L,R=301]
Updated answer (2013-03-18):
# Redirect feedback.domain.de to http://www.domain.de/de/abc/cde.html
RewriteCond %{HTTP_HOST} ^feedback\.(domain\.(de))$
RewriteRule .+ http://www.%1/%2/feed.html [L,R=301]
# domain.de -> www.domain.de/de
RewriteCond %{HTTP_HOST} ^(domain\.(de))$
RewriteRule .+ http://www.%1/%2/$0 [L,R=301]
# www.domain.de/de -> www.domain.de/de/index.html
RewriteCond %{HTTP_HOST} ^www\.domain\.(de)$
RewriteRule ^%1/?$ index.html [L,R=301]

.htaccess simple redirection and too many rediercections: how to solve that?

my site (domain.com) is now located into /var/www/
and you can also access it directly through its IP: 88.88.88.88
I would like to obtain:
when user enter the IP : 88.88.88.88, it redirects to 88.88.88.88/reboot/
if user enters the full domain name, it shows the site as it does now.
I tried with .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} 88.88.88.88
RewriteRule .* http://88.88.88.88/reboot/ [R=301,L]
RewriteCond %{HTTP_HOST} ^mysite.com
RewriteRule .* http://www.mysite.com [R=301,L]
does not work good:
www.mysite.com :GOOD
mysite.com: GOOD
88.88.88.88 ->redirects to 88.88.88.88/reboot : GOOD
but then it generates an error: Error 310 (net::ERR_TOO_MANY_REDIRECTS)
Any clue ?
When you're on 88.88.88.88/reboot, your HTTP_HOST is still equal to 88.88.88.88.
Try adding a RewriteCond rule depending on the REQUEST_URI, which will redirect everything that is not /reboot to /reboot :
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} 88.88.88.88
RewriteCond %{REQUEST_URI} !^/reboot/
RewriteRule ^(.*)$ http://88.88.88.88/reboot/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^mysite.com
RewriteRule .* http://www.mysite.com [R=301,L]
The ERR_TOO_MANY_REDIRECTS comes because your rule loops.
Whenever a request comes to host 88.88.88.88 , you redirect it to 88.88.88.88/reboot/ , but /reboot/ uri is also on that host, so it redirects again and loops.
If you only want to redirect / to /reboot/ on host 88.88.88.88 , you need to alter your RewriteRule to this:
RewriteCond %{HTTP_HOST} 88.88.88.88
RewriteRule ^/$ http://88.88.88.88/reboot/ [R=301,L]
If, however, you wish to redirect everything outside of /reboot/ , then you need to add a condition (RewriteCond):
RewriteCond %{HTTP_HOST} 88.88.88.88
RewriteCond %{REQUEST_URI} !^/reboot/
RewriteRule ^/$ http://88.88.88.88/reboot/ [R=301,L]
(keep the rest of the rules intact).

Resources