2013-04-13 55 views
0

我正在使用SSL证书运行网站。htaccess规范名称重定向

为了优化我的搜索引擎优化,我想将所有的CNames重定向到https://example.com

有相同的地址的4种变体: HTTPS://示例.com HTTPS://www.example.com中 HTTP://示例.com HTTP://www.example.com中

这段代码几乎工作:

RewriteCond %{HTTP_HOST} ^www\.example\.com$ [OR] 
RewriteCond %{HTTPS} =on 
RewriteRule ^.*$ https://example.com/$0 [R,L] 

然而,其中一个网址,不要重定向。如果我只输入example.com(http://example.com),则URL不会重定向到https://example.com

任何人都可以帮助我与上面的代码纠正这一点?

回应乔恩下面的答案。我将代码片段添加到现有的.htaccess文件中。由于我不熟悉.htaccess,我想知道该代码片段的代码是否会导致重定向循环?

这里包括下面的代码片段的.htaccess文件:

<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> 
+0

的[URL规范不重定向]可能重复(http://stackoverflow.com/questions/12763432/canonical-url-not-重定向) –

回答

0

的HTTPS应为“关闭”

RewriteCond %{HTTP_HOST} ^www\.example\.com$ [OR] 
RewriteCond %{HTTPS} off 
RewriteRule ^.*$ https://example.com/$0 [R,L] 

因为你想要么主机与WWW开头或HTTPS关闭。

+0

感谢您的答案,但是当我添加并导航到他的网站我收到一个错误,说有一个“重定向循环”? –

+0

@DougFirr不知道为什么你会得到太多的重定向,这些规则适合我。看看你的htaccess文件,你会希望把这些规则**放在**规则的其余部分之前。 –

+0

再次感谢您的反馈。尝试将片段移动到.htaccess文件的开头,但结果相同:-( 我想知道是否是CMS配置的后果。我知道int他的配置文件中有一行代码使用https或http,this是想在后端进行管理,嗯,可能会尝试将其关闭并看到它会发生什么 –

1

这里是另一种选择:

# http www and non-www to https non-www 
RewriteCond %{HTTPS} off 
RewriteCond %{HTTP_HOST} (?:www\.)?(.*) [NC] 
RewriteRule ^(.*) https://%1/$1   [R=301,L] 

# https www to non-www 
RewriteCond %{HTTPS} on 
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC] 
RewriteRule ^(.*) https://%1/$1  [R=301,L] 

我想最好的地方,这个代码是低于RewriteBase /指令。

如果存在循环,它很难由前面的代码生成。也许由第二规则,您可以尝试添加一个条件,如:

# Add next line before the rule 
RewriteCond %{REQUEST_URI} !index\.php [NC] 
# Current rule 
RewriteRule .* index.php?kohana_uri=$0 [L,QSA] 
+0

感谢您的反馈,但反馈回路仍然存在,我确实做了一些小动作,并认为我已经开始工作了,我将在此发布一个新问题。 –