【发布时间】:2013-04-06 04:36:15
【问题描述】:
我正在使用 SSL 证书运行一个网站。
为了优化我的 SEO,我想将所有 CName 重定向到 https://example.com。
同一地址有 4 种变体: https://example.com https://www.example.com http://example.com http://www.example.com
这段代码几乎可以工作:
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [OR]
RewriteCond %{HTTPS} =on
RewriteRule ^.*$ https://example.com/$0 [R,L]
但是,其中一个 URL 不会重定向。如果我只输入 example.com (http://example.com),则 URL 不会重定向到 https://example.com。
谁能帮我用上面的代码纠正这个问题?
回应下面乔恩的回答。我将代码的 sn-p 添加到现有的 .htaccess 文件中。由于我不熟悉 .htaccess,我想知道该 sn-p 周围的代码是否导致了重定向循环?
这是 .htaccess 文件,包括下面的 sn-p:
<IfModule mod_rewrite.c>
RewriteEngine On
# Installation directory
RewriteBase /
# Protect application and system files from being viewed
RewriteRule ^(application|modules|system|tests|sql) - [F,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php?kohana_uri=$0 [L,QSA]
#redirect CNames to non www
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^.*$ https://example.com/$0 [R,L]
</IfModule>
【问题讨论】:
标签: .htaccess rewrite http-redirect