I'm looking for the most efficient way to redirect incoming urls to the https: version of our site. What we have in place works (see below), I'm just wondering if there's a better way to do it, and to minimize the number of redirects.
Redirecting should cover the following cases:
(Domain level)
http://www.example.com - > https://www.example.com
http://www.example.com/dir -> https://www.example.com/dir
http://example.com -> https://www.example.com
http://example.com/dir -> https://www.example.com/dir
www.example.com -> https://www.example.com
www.example.com/dir -> https://www.example.com/dir
example.com -> https://www.example.com
example.com/dir -> https://www.example.com/dir
(Subdomains)
http://subdomain.example.com -> https://www.example.com/subdomain
subdomain.example.com -> https://www.example.com/subdomain
Here's what we have in our htaccess file:
# Rewrite subdomains to folders, add www if not present
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
# Rewrite anything not previously caught
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Thanks in advance!
By your rules you forced both main-domain and sub-domain into https but only non-www of main-domain into wwww so , to get it work for both do this :
RewriteCond %{HTTP_HOST} !^www [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Note: clear browser cache the test
Related
I want to redirect an old domain (https & www) to the main one. I need to redirect it to the home page and blog. But, there are 4 pages on the old site I want to redirect to the subdomain of the main page.
How it should look:
Oldwebsite1.com -> mainwebsite2.com
Oldwebsite1.com/blog -> Mainwebsite2.com/blog
Oldwebsite1.com/category1 -> new.mainwebsite2.com/category1
Oldwebsite1.com/category2 -> new.mainwebsite2.com/category2 /// etc...
Will typical code like this work or do I need to adjust it?
#Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ http://www.mainwebsite2.com/$1 [R=301,L]
Based on your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs. These also handles optional www in your host name too.
RewriteEngine ON
##For Oldwebsite1.com -> mainwebsite2.com
RewriteCond %{HTTP_HOST} ^(?:www\.)Oldwebsite1\.com$ [NC]
RewriteRule ^/?$ http://mainwebsite2.com [R=301,L]
##For Oldwebsite1.com/blog -> Mainwebsite2.com/blog
RewriteCond %{HTTP_HOST} ^(?:www\.)Oldwebsite1\.com$ [NC]
RewriteRule ^(blog)/?$ http://mainwebsite2.com/$1 [NC,R=301,L]
##For Oldwebsite1.com/category1 -> new.mainwebsite2.com/category1 OR Oldwebsite1.com/category2 -> new.mainwebsite2.com/category2
RewriteCond %{HTTP_HOST} ^(?:www\.)Oldwebsite1\.com$ [NC]
RewriteRule ^(category1|category2)/?$ http://new.mainwebsite2.com/$1 [NC,R=301,L]
I am trying to code an htaccess file that covers all the bases that I need, but I have a lot of quite specific actions to cover.
I have three domains: .com .co.uk and .london and I want all traffic to end up at https://www.somepage.com except any traffic to .London, which has to be redirected to a specific mysite/London.php page
So all my conditions are:
force www prefix
force https
remove extensions
force .co.uk to .com domain
force any .london traffic to specific /london page
So far I have:
# Force WWW prefix
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# Force SSL
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# Remove .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
So I need to add the last two conditions
force .co.uk to .com domain
force any .london traffic to specific /london page
But I am having difficulty getting everything to work together.
EDIT:
Still struggling with getting all conditions to work together, so adding some examples here for clarity.
www.example.com -> https://www.example.com
http://example.com -> https://www.example.com
http://www.example.com/mypage.php -> https://www.example.com/mypage
www.example.co.uk -> https://www.example.com
http://www.example.co.uk -> https://www.example.com
http://www.example.co.uk/mypage.php -> https://www.example.com/mypage
www.example.london -> https://www.example.com/london
http://example.london -> https://www.example.com/london
http://example.london/anything -> https://www.example.com/london
So ALL URLS must end up with 'www' added, .com domain only, https forced, php extension removed.
I have tried literally hundreds of code snippets and combinations, but cant get all conditions to work at the same time.
You can use these rules :
# force .co.uk to .com domain
RewriteCond %{HTTP_HOST} ^(www\.)?\.example\.co\.uk$
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R=301]
# force any .london traffic to specific /london page
RewriteCond %{HTTP_HOST} ^(www\.)?example\.london$
# this line is important to avoid Redirect loop error
RewriteCond %{REQUEST_URI} !/london/page
RewriteRule ^ /london/page%{REQUEST_URI} [L,R]
# Force WWW prefix
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# Force SSL
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# Remove .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
I have Drupal 7 site with default .htaccess file. I need redirect all site possibilities to one https site. What I need is:
http://example.com -> https://example.com
http://www.example.com -> https://example.com
https://www.example.com -> https://example.com
I have tried many options but I still get error: "This Webpage has a redirect loop."
Default .htaccess looks like:
.htaccess
[Edited] I found solution:
for remove www and retirect to https: RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]<br>
RewriteRule ^ https%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301]
for redirect non www to https:
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,QSA,L]
You can also try setting the base_url setting in settings.php. By default it's commented out, but you can uncomment it and set it with the desired protocol:
$base_url = 'https://'.$_SERVER['SERVER_NAME']; // NO trailing slash!
This has saved me on multiple occasions, in particular when trying to force HTTPS from behind a proxy server, and using the Domain Access module in Drupal 7. I found the solution here.
Maybe someone is interested in these redirections (to https AND to www.*):
http://example.com to https://www.example.com
and
http://www.example.com to https://www.example.com
.htaccess:
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
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]
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...