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

标签 php apache .htaccess mod-rewrite codeigniter

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

相关文章:

security - 转到服务器的根目录时,如何从 apache2 禁用目录索引?

php - 奇怪的 PHP 字符集问题?

wordpress - 在 Wordpress 上强制 Cloud Flare Flexible SSL,提要除外

php - 在 mac : phpize not working 上的 xampp 上安装 xDebug

apache - 漏洞?没有托管策略无法设置持久 bool 值

PHP PDO,排除具有空字段值的行

php - apache 可以从 PHP 写入但不会提供文件的目录

.htaccess - 使用 .htaccess 不重写具有特定 GET 参数的 URL

php - Apache错误日志: Log only for part of server

javascript - 如何使用一些前缀进行自动增量