Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

php - Move to https://www, when it is www or http:// or without www and http://?

I am using Zend framework where i have nice looking url controllers. Following .htaccess is working but its making SEO to see us as four links for one page.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]

I need to do following fix using htaccess:

www.stackoverflow.com/nice-looking-url = https://www.stackoverflow.com/nice-looking-url

stackoverflow.com/nice-looking-url = https://www.stackoverflow.com/nice-looking-url

http://www.stackoverflow.com/nice-looking-url = https://www.stackoverflow.com/nice-looking-url

http://stackoverflow.com/nice-looking-url = https://www.stackoverflow.com/nice-looking-url

How to do it correctly using htaccess? so that above input urls are safely landing with https://www. in front always?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Have your full .htaccess like this:

RewriteEngine On

## add www and turn on https in same rule - main domain
RewriteCond %{HTTP_HOST} !^www. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www.)?(example.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

## turn on https in same rule - sub domain
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

Make sure to use a new browser to test your changes to avoid old browser cache.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...