2010-12-02 69 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'] = ""; 
相關問題