英文:
.htaccess redirection does not work for https:// to https://www
问题
You can make the following changes to your htaccess file to ensure correct redirection from https://example.com
to https://www.example.com
:
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Redirect http://example.com to https://www.example.com
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301]
# Redirect non-www https://example.com to https://www.example.com
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301]
# Rewrite everything else to index.html to allow HTML5 state links
RewriteRule ^ index.html [L]
These changes should ensure that both https://example.com
and https://example.com/public/home
correctly redirect to https://www.example.com
and https://www.example.com/public/home
, respectively.
英文:
I currently have a website built on Angular and hosted on Apache2 on a Ubuntu server and recently changed the SSL certificate. I keep getting 'NET::ERR_CERT_COMMON_NAME_INVALID' if I go to https://example.com/public/home
but no error when going to https://www.example.com/public/home
.
One strange thing is if I write the URL as https://example.com/public/home/
, it properly redirects to https://www.example.com/public/home
. Also, going to example.com
redirects to the correct one.
I've shared below my current htaccess:
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]
#http to https
RewriteEngine On
RewriteCond %{HTTP_HOST} example.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
What should I change for the correct redirection for https to https www?
答案1
得分: 1
已在获得example.com和www.example.com的SSL证书后解决,感谢John Hanley的评论。谢谢。
英文:
Resolved after getting an SSL certificate for both example.com and www.example.com after John Hanley's comment. Thanks.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论