url 未正确重定向到规范

发布于 2025-01-13 04:56:20 字数 1084 浏览 2 评论 0 原文

我的网站使用 .htaccess 重定向,如下所示:

RewriteEngine On    
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

问题是

警告,没有 301 重定向来将流量重定向到您的首选域。带 www 和不带 www 的页面均可成功加载。被视为重复内容! 并非页面的所有版本都指向相同的 URL。

URL解析的URL:

  1. http://example.com/inner-page 重定向到 https://example.com/inner-page
  2. http://www.example.com/inner-page 重定向到 https://example.com/index.php/inner-page
  3. https://example.com/inner-page 重定向到 https://example.com/inner-page
  4. https://www.example.com/inner-page 重定向至
    https://example.com/index.php/inner-page

为什么相同的 URL 在浏览器中表现不同。我应该在 .htaccess 文件中更改哪些内容,以便它可以重定向到正确的 URL?我想知道这种奇怪的行为怎么会发生?

解决方案是什么,以便他们都重定向到仅

https://example.com/inner-page

任何帮助将受到高度赞赏。

My website uses .htaccess redirect as below:

RewriteEngine On    
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

The issue is that

Warning, no 301 redirects are in place to redirect traffic to your preferred domain. Pages that load successfully both with and without www. are treated as duplicate content!
Not all versions of your page point to the same URL.

URL Resolved URL:

  1. http://example.com/inner-page redirects to https://example.com/inner-page
  2. http://www.example.com/inner-page redirects to
    https://example.com/index.php/inner-page
  3. https://example.com/inner-page redirects to
    https://example.com/inner-page
  4. https://www.example.com/inner-page redirects to
    https://example.com/index.php/inner-page

Why the same URL is behaving differently with browser. What should I change in the .htaccess file so that it can redirect to the proper URL? I'm wondering how this strange behaviour can happen?

What is the solution so that they all redirect to only

https://example.com/inner-page

any help will be highly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

尹雨沫 2025-01-20 04:56:20

tl;dr 你的规则顺序错误。您正在重定向到 HTTP,而不是 HTTPS。您并未专门将 HTTP 重定向到 HTTPS。


  1. http://example.com/inner-page 重定向到 https://example.com/inner-page

此重定向不是由 .htaccess 执行的 您发布的指令。这可能是由您的 PHP/应用程序触发的。

.htaccess 中的规则仅重定向 www.example.com,而不是 example.com

  • http://www.example.com/inner-page 重定向到 https://example.com/index.php/inner-page
  • 因为您的规则位于错误的顺序。第一条规则首先将请求重写为 /index.php/inner-page,然后重定向该 URL。但这实际上是 2 个重定向,因为您发布的指令重定向到 HTTP,而不是 HTTPS。

    您的第二条规则将触发重定向到 http://example.com/index.php/inner-page (注意 HTTP),并且您的 PHP/应用程序可能会将其重定向到 HTTPS(见上文)。

  • https://example.com/inner-page 重定向到 https://example.com/inner-page
  • 这不是重定向。在这种情况下什么也没有发生。 (这是正确的。)

  • https://www.example.com/inner-page 重定向到
    https://example.com/index.php/inner-page
  • 与上面#2 相同。 (除非您首先将请求重定向回 HTTP,并且您依赖 PHP/应用程序重定向回 HTTPS - 请参阅上文。)

    您当前的规则不会专门将 HTTP 重定向到 HTTPS,它们仅将 www 重定向到非 www。这个应该重定向到HTTPS,而不是HTTP。

    请尝试以下操作:

    RewriteEngine On
    
    # Redirect www to non-www (+ HTTPS)
    RewriteCond %{HTTP_HOST} ^www\.(example\.com) [NC]
    RewriteRule (.*) https://%1/$1 [R=301,L]
    
    # HTTP to HTTPS
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
    
    # Front-controller
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*) index.php/$1 [L]
    

    HTTP 到 HTTPS 重定向假设 SSL 证书由您的应用程序服务器管理,即。您没有使用 CDN、负载均衡器、前端缓存代理等。(否则这可能会触发重定向循环。)

    首先使用 302(临时)重定向进行测试,以避免潜在的缓存问题。

    在测试之前您需要清除浏览器缓存。

    tl;dr Your rules are in the wrong order. You are redirecting to HTTP, not HTTPS. You are not specifically redirecting HTTP to HTTPS.


    1. http://example.com/inner-page redirects to https://example.com/inner-page

    This redirect is not performed by the .htaccess directives you've posted. This is probably triggered by your PHP/application.

    Your rule in .htaccess only redirects www.example.com, not example.com.

    1. http://www.example.com/inner-page redirects to https://example.com/index.php/inner-page

    Because your rules are in the wrong order. The first rule rewrites the request to /index.php/inner-page first and it's this URL that is then being redirected. But this is actually 2 redirects, since the directives you've posted redirect to HTTP, not HTTPS.

    Your 2nd rule will trigger a redirect to http://example.com/index.php/inner-page (note HTTP) and your PHP/application is probably redirecting this to HTTPS (see above).

    1. https://example.com/inner-page redirects to https://example.com/inner-page

    That's not a redirect. Nothing is happening in this scenario. (Which is correct.)

    1. https://www.example.com/inner-page redirects to
      https://example.com/index.php/inner-page

    Same as #2 above. (Except you are redirecting the request back to HTTP first and you are relying on PHP/application redirecting back to HTTPS - see above.)

    Your current rules do not specifically redirect HTTP to HTTPS, they only redirect www to non-www. And this should redirect to HTTPS, not HTTP.

    Try the following instead:

    RewriteEngine On
    
    # Redirect www to non-www (+ HTTPS)
    RewriteCond %{HTTP_HOST} ^www\.(example\.com) [NC]
    RewriteRule (.*) https://%1/$1 [R=301,L]
    
    # HTTP to HTTPS
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
    
    # Front-controller
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*) index.php/$1 [L]
    

    The HTTP to HTTPS redirect assume the SSL cert is managed by your application server, ie. you are not using a CDN, load balancer, front-end caching proxy etc. (Otherwise this might trigger a redirect loop.)

    Test first with 302 (temporary) redirects to avoid potential caching issues.

    You will need to clear your browser cache before testing.

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