我正在运行 CodeIgniter 平台,它使用 .htaccess 来接受 URL,例如
http://www.mysite.com/controller/function/argument
我目前使用一些 .htaccess 重写,即(简化):
RewriteEngine On
RewriteCond %{REQUEST_URI} !^(/index\.php|/images|/assets)
RewriteRule ^(.*)$ /index.php/$1 [L]
我想添加一个重写规则,将所有非 www 请求重定向到 www。我还希望域名后面的 URI 字符串在重定向中保持不变。例如,如果用户请求 http://mysite.com/controller/function/argument
,我希望 .htaccess 文件在浏览器中将请求重写为 http://www.mysite.com/controller/function/argument
然后处理请求。
最佳答案
我遇到了类似的问题,这个 .htaccess 对我有用
RewriteEngine On
#This bit rewrites your host name to include www
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,NC,L]
#This bit does the codeigniter magic
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
关于php - .htaccess 将非 WWW 重定向到 WWW 保留 URI 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1685962/