2010-12-02 76 views
0

无论出于何种原因,我似乎无法得到正确的结果,我在这里和apache的网站上查看了许多示例。我试图强制www.domain.com而不是域名在http或https上,但我不想通过http强制https。我如何在https和http上强制使用www子域名

以下代码似乎适用于所有https连接,但http不会重定向到www。

RewriteEngine On 
RewriteCond %{HTTPS} on 
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC] 
RewriteRule^https://www.domain.com%{REQUEST_URI} [R=301] 

RewriteEngine On 
RewriteCond %{HTTPS} off 
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC] 
RewriteRule^http://www.domain.com%{REQUEST_URI} [R=301] 
+0

你可能会得到更好的反应,要求在http://webmasters.stackexchange.com/ – 2010-12-02 12:56:02

回答

1
  1. 你不需要第二RewriteEngine指令。这可能会导致解析问题,也可能不会导致第二组规则无效。要测试是否属于这种情况,请尝试切换两个块的顺序。
  2. 使用L修改绝对是最后一个的请求是个好习惯。因此,两次出现[R=301][R=301,L]
  3. 主要由于风格问题,我会考虑改变RewriteRule指令来像(使用HTTP或HTTPS如适用):

    RewriteRule ^(.*)$ http://www.domain.com $1 [R=301,L,QSA]

1

你的规则似乎是罚款。可以按如下方式将它们组合起来:

RewriteCond %{HTTP_HOST} !^www\.example\.com$ 
RewriteCond %{HTTPS}s on(s)| 
RewriteRule^http%1://www.example.com%{REQUEST_URI} [L,R=301] 

还要注意另外大号标志已应用此规则后停止重写的过程。

+0

这似乎喜欢它应该工作,但只为https。我决定删除第二个条件,以便不管https是否将www子域名重写为http。看起来我制作的任何规则都是在http下被忽视的。基本上任何http连接都忽略了这些规则。有什么想法吗? – Brian 2010-12-02 22:56:19

0

万一任何人仍然需要这个答案。使用另一个.htaccess。从这里得到指导,我发现它和它看起来不错:http://www.farinspace.com/codeigniter-htaccess-file/

<IfModule mod_rewrite.c> 

    RewriteEngine On 
    RewriteBase/

    ### Canonicalize codeigniter URLs 

    # If your default controller is something other than 
    # "welcome" you should probably change this 
    RewriteRule ^(welcome(/index)?|index(\.php)?)/?$/[L,R=301] 
    RewriteRule ^(.*)/index/?$ $1 [L,R=301] 

    # Removes trailing slashes (prevents SEO duplicate content issues) 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^(.+)/$ $1 [L,R=301] 

    # Enforce www 
    # If you have subdomains, you can add them to 
    # the list using the "|" (OR) regex operator 
    RewriteCond %{HTTP_HOST} !^(www|subdomain) [NC] 
    RewriteRule ^(.*)$ http://www.domain.tld/$1 [L,R=301] 

    # Enforce NO www 
    #RewriteCond %{HTTP_HOST} ^www [NC] 
    #RewriteRule ^(.*)$ http://domain.tld/$1 [L,R=301] 

    ### 

    # Removes access to the system folder by users. 
    # Additionally this will allow you to create a System.php controller, 
    # previously this would not have been possible. 
    # 'system' can be replaced if you have renamed your system folder. 
    RewriteCond %{REQUEST_URI} ^system.* 
    RewriteRule ^(.*)$ /index.php/$1 [L] 

    # Checks to see if the user is attempting to access a valid file, 
    # such as an image or css document, if this isn't true it sends the 
    # request to index.php 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^(.*)$ index.php/$1 [L] 

</IfModule> 

<IfModule !mod_rewrite.c> 

    # Without mod_rewrite, route 404's to the front controller 
    ErrorDocument 404 /index.php 

</IfModule> 

记住,一旦你有你的CodeIgniter htaccess文件设置,你将要进入你的“/system/application/config/config.php ”,找到以下内容:

$config['index_page'] = "index.php"; 

并将其更改为:

$config['index_page'] = ""; 
相关问题