简体   繁体   中英

How can I redirect all pages from www to non-www and also http to https with htaccess?

I have been able to find solutions to each of these redirects separately but I can't find something that does both correctly.

I need the website to redirect from:

http://somesite.com.au to https://somesite.com.au
http://www.somesite.com.au to https://somesite.com.au
https://www.somesite.com.au to https://somesite.com.au

The main combination that I'm having trouble with is:

https://www.somesite.com.au to https://somesite.com.au

Currently I am using the following in my htaccess file:

# .htaccess main domain to subdirectory redirect 
# Do not change this line. 
RewriteEngine on

# Ensure we are using HTTPS version of the site.
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Redirect www to non-ww
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

What should I be using in my htaccess to make this work?

You're causing a loop because your www rule redirects to http:// . Just change it to redirect to https:// instead:

# Redirect www to non-ww
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM