2015-04-12 117 views
0

我正在使用codeigniter.I想要将所有用户从www重定向到非www。 我的意思是,如果用户不来http://www.example.com的请求应该被重定向到http://example.comhttp://www.example.com/url/1/2http://example.com/url/1/2Codeigniter htaccess重定向到非www

我怎样才能实现这一目标?

我htacess代码:

RewriteEngine On 
RewriteCond %{REQUEST_URI} ^/system.* 
RewriteRule ^(.*)$ index.php/?$1 [QSA,L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.+)$ index.php/?$1 [QSA,L] 

回答

2

尝试这种方式,

RewriteEngine On 
RewriteCond %{REQUEST_URI} ^/system.* 
RewriteRule ^(.*)$ index.php/?$1 [QSA,L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteBase/
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] 
RewriteRule ^(.*)$ http://%1/$1 [R=301,L] 
+0

你为什么要删除此行重写规则^ $的index.php/$ 1 [(*)? QSA,L]?我不想在url中使用index.php。 – Okan

+0

好的,这只是在输入时忽略。 –

+0

我得到404错误。对于此网址:example.com/abc – Okan

0
RewriteEngine on 
    RewriteBase/

    #for redirect non www to www 

    RewriteCond %{HTTP_HOST} !^www\. [NC] 
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 

    #for redirect www to non www 

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

    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^(.*)$ index.php?/$1 [L] 

    #Also for http to https redirect 

    RewriteEngine On 
    RewriteCond %{HTTPS} !=on 
    RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]