.htaccess重定向不适用于从https://到https://www

huangapple go评论58阅读模式
英文:

.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.

huangapple
  • 本文由 发表于 2023年5月22日 12:52:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76303115.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定