繁体   English   中英

.htaccess将非WWW重定向到WWW保留URI字符串

[英].htaccess Redirect non-WWW to WWW preserving URI string

我正在运行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]

它应该是这样的:

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

如果您使用的是apache 2.4,则可以使用以下简单重定向:

<if "%{HTTP_HOST} =='example.com'">
Redirect / http://www.example.com/
</if>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM