2014-08-29 70 views
0

目前我有htaccess從URL中刪除index.php,其工作正常。但現在我想強制網址以www開頭。htaccess強制www和刪除index.php

我當前的代碼:

DirectoryIndex index.php 
RewriteEngine on 
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico) 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA] 

我嘗試下面的代碼,但它不工作:

DirectoryIndex index.php 
RewriteEngine on 
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico) 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{HTTP_HOST} !^$ 
RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteCond %{HTTPS}s ^on(s)| 
#RewriteRule ^(.*)$ /index.php?/$1 [L] 
RewriteRule^http%1://www.%{HTTP_HOST}%{REQUEST_URI}/index.php?/$1 [R=301,L] 

但其沒有工作,我用你的規則笨

+0

[Codeigniter htaccess刪除index.php和www]的可能重複(http://stackoverflow.com/questions/12030990/codeigniter-htaccess-to-remove-index-php-and-www) – Oberst 2014-08-29 17:28:11

+1

這個問題是不一樣的,這裏我要求不要刪除www,而是強制要求www。請正確閱讀 – 2014-08-29 17:32:28

+0

否...不是完全重複,但答案仍然存在。 'RewriteCond%{HTTP_HOST}^domain.com [NC] RewriteRule ^(。*)$ http://www.domain.com/$1 [L,R = 301]' – Oberst 2014-08-29 17:36:23

回答

1

更改訂單並在重寫規則之前保持重定向:

DirectoryIndex index.php 
RewriteEngine on 

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

RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico) 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA] 
+0

感謝此工作 – 2014-08-29 17:33:03

+0

不客氣,很高興它解決了。 – anubhava 2014-08-29 17:42:25