我想将我的所有网站流量重定向到“https://www.example.com/index.html”
如果有人访问我的网站,例如:
example.com
www.example.com
http://example.com
http:// www.example.com
https://example.com
https://www.example.com
我想将其重定向到 https: //www.example.com/index.html
。我尝试了下面的代码:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mywebsite.com/index.html$1 [R=301,L,NE]
上面的代码工作正常,但是如果访问者输入 https://www.example.com
它不会重定向到 https://www.example.com/index .html
并且如果访问者使用 Google chrome 并输入 www.example.com
它也不会重定向到 https://www.example.com/index.html< /代码>。
If someone visits my website like:
example.com
www.example.com
http://example.com
http://www.example.com
https://example.com
https://www.example.com
I want to redirect it to https://www.example.com/index.html
. I tried the code below:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mywebsite.com/index.html$1 [R=301,L,NE]
The above code worked fine however if visitor type https://www.example.com
it will not redirect to https://www.example.com/index.html
and also if visitor uses Google chrome and type www.example.com
it will also not redirect to https://www.example.com/index.html
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请尝试以下操作:
仅第一条规则即可满足您问题中概述的要求。所有指定的 URL 都会重定向到
https://www.example.com/index.html
(HTTPS + www)。第二条和第三条规则只是其他任何内容的通用规范(www + HTTPS)重定向,包括用户请求
/index.html
的情况。首先使用 302(临时)重定向进行测试以避免潜在的缓存问题。在测试之前您需要清除浏览器缓存。
看看你的规则...
您的第一条规则重定向到 HTTP,而不是 HTTPS,这是预期的目标。
第二条规则将所有内容(包括任何静态资源)重定向到
/index.html
并在末尾附加请求的 URL(使用$1
代码>反向引用)。例如。对http://example.com/foo
的请求将被重定向到https://www.example.com/index.htmlfoo
(分两次重定向)。更新:
要在 localhost 上禁用所有这些规则,您可以首先在
RewriteEngine
指令之后添加以下规则。上面的状态...对于每个 URL 请求,其中
Host
标头完全等于localhost
,然后不执行替换(如连字符-
) 和L
(last
) 标志会阻止处理以下规则。Try the following instead:
Just the first rule satisfies the requirements outlined in your question. All the stated URLs are redirected to
https://www.example.com/index.html
(HTTPS + www).The second and third rules are just generic canonical (www + HTTPS) redirects for anything else, including if the user requests
/index.html
.Test first with a 302 (temporary) redirect to avoid potential caching issues. You will need to clear your browser cache before testing.
A look at your rules...
Your first rule redirects to HTTP, not HTTPS, which is the intended target.
The second rule redirects everything (including any static assets) to
/index.html
and appends the requested URL on the end (with the use of the$1
backreference). eg. A request forhttp://example.com/foo
would be redirected tohttps://www.example.com/index.htmlfoo
(in two redirects).UPDATE:
To disable all these rules on localhost you can add the following rule first, immediately after the
RewriteEngine
directive.The above states... for every URL request, where the
Host
header is exactly equal tolocalhost
then perform no substitution (as denoted by the hyphen-
) and theL
(last
) flag prevents the following rules being processed.