htaccess force www and remove index.php

2019-09-09 00:05发布

Currently I have htaccess which removes index.php from URL, its working fine. But now I want to force url to start with www.

My current code:

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]

I tried following code, but its not working:

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]

But its not working, I am using codeigniter

1条回答
ゆ 、 Hurt°
2楼-- · 2019-09-09 00:46

Change order of your rule and keep redirects before rewrite rules:

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]
查看更多
登录 后发表回答