我的网站使用 .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:
http://example.com/inner-page
重定向到 https://example.com/inner-page
http://www.example.com/inner-page
重定向到
https://example.com/index.php/inner-page
https://example.com/inner-page
重定向到
https://example.com/inner-page
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:
http://example.com/inner-page
redirects to https://example.com/inner-page
http://www.example.com/inner-page
redirects to
https://example.com/index.php/inner-page
https://example.com/inner-page
redirects to
https://example.com/inner-page
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.
发布评论
评论(1)
tl;dr 你的规则顺序错误。您正在重定向到 HTTP,而不是 HTTPS。您并未专门将 HTTP 重定向到 HTTPS。
此重定向不是由
.htaccess 执行的
您发布的指令。这可能是由您的 PHP/应用程序触发的。.htaccess
中的规则仅重定向www.example.com
,而不是example.com
。因为您的规则位于错误的顺序。第一条规则首先将请求重写为
/index.php/inner-page
,然后重定向该 URL。但这实际上是 2 个重定向,因为您发布的指令重定向到 HTTP,而不是 HTTPS。您的第二条规则将触发重定向到
http://example.com/index.php/inner-page
(注意 HTTP),并且您的 PHP/应用程序可能会将其重定向到 HTTPS(见上文)。这不是重定向。在这种情况下什么也没有发生。 (这是正确的。)
与上面#2 相同。 (除非您首先将请求重定向回 HTTP,并且您依赖 PHP/应用程序重定向回 HTTPS - 请参阅上文。)
您当前的规则不会专门将 HTTP 重定向到 HTTPS,它们仅将 www 重定向到非 www。这个应该重定向到HTTPS,而不是HTTP。
请尝试以下操作:
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.
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 redirectswww.example.com
, notexample.com
.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).That's not a redirect. Nothing is happening in this scenario. (Which is correct.)
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:
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.