我想将我的所有网站流量重定向到“https://www.example.com/index.html”

发布于 2025-01-11 14:14:04 字数 855 浏览 2 评论 0原文

如果有人访问我的网站,例如:

  • 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巴黎盛开的樱花 2025-01-18 14:14:04

请尝试以下操作:

RewriteEngine On

# Redirect "/" to "/index.html" (HTTPS + www)
RewriteRule ^$ https://www.example.com/index.html [R=301,L]

# Redirect non-www to www (+HTTPS)
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Redirect HTTP to HTTPS (any remaining URLs)
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

仅第一条规则即可满足您问题中概述的要求。所有指定的 URL 都会重定向到 https://www.example.com/index.html (HTTPS + www)。

第二条和第三条规则只是其他任何内容的通用规范(www + HTTPS)重定向,包括用户请求 /index.html 的情况。

首先使用 302(临时)重定向进行测试以避免潜在的缓存问题。在测试之前您需要清除浏览器缓存。


看看你的规则...

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/index.html$1 [R=301,L,NE]

您的第一条规则重定向到 HTTP,而不是 HTTPS,这是预期的目标。

第二条规则将所有内容(包括任何静态资源)重定向到 /index.html 并在末尾附加请求的 URL(使用 $1代码>反向引用)。例如。对 http://example.com/foo 的请求将被重定向到 https://www.example.com/index.htmlfoo (分两次重定向)。


更新:

每次我在本地主机环境中工作时,本地主机都会受到 HTTPS 重定向的影响。 ...我在 htaccess 文件中没有其他规则,只有这个重定向规则。是的,可以在本地主机上关闭所有这些规则。我的本地开发环境中的主机名是“localhost”。

要在 localhost 上禁用所有这些规则,您可以首先在 RewriteEngine 指令之后添加以下规则。

# Prevent further processing on localhost
RewriteCond %{HTTP_HOST} =localhost
RewriteRule ^ - [L]

上面的状态...对于每个 URL 请求,其中 Host 标头完全等于 localhost,然后不执行替换(如连字符 -) 和 L (last) 标志会阻止处理以下规则。

Try the following instead:

RewriteEngine On

# Redirect "/" to "/index.html" (HTTPS + www)
RewriteRule ^$ https://www.example.com/index.html [R=301,L]

# Redirect non-www to www (+HTTPS)
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Redirect HTTP to HTTPS (any remaining URLs)
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

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

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/index.html$1 [R=301,L,NE]

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 for http://example.com/foo would be redirected to https://www.example.com/index.htmlfoo (in two redirects).


UPDATE:

Every time I work in a localhost environment the localhost is affected by the HTTPS redirection. ... I have no other rules in the htaccess files, only this redirection rules only. Yes it's okay to turn off all these rules on localhost. The hostname on my local dev environment is "localhost".

To disable all these rules on localhost you can add the following rule first, immediately after the RewriteEngine directive.

# Prevent further processing on localhost
RewriteCond %{HTTP_HOST} =localhost
RewriteRule ^ - [L]

The above states... for every URL request, where the Host header is exactly equal to localhost then perform no substitution (as denoted by the hyphen -) and the L (last) flag prevents the following rules being processed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文