将CodeIgniter URL重定向到WWW时出现问题

将CodeIgniter URL重定向到WWW时出现问题,codeigniter,mod-rewrite,url-rewriting,Codeigniter,Mod Rewrite,Url Rewriting,我有一个CI应用程序,它使用.htaccess进行URL路由。我的基本设置如下: RewriteRule ^$ /var/www/html/ferdy/jungledragon/index.php [L] RewriteCond $1 !^(index\.php|images|img|css|js|swf|type|themes|robots\.txt|favicon\.ico|sitemap\.xml) RewriteRule ^(.*)$ /var/www/html/ferdy/jungle

我有一个CI应用程序,它使用.htaccess进行URL路由。我的基本设置如下:

RewriteRule ^$ /var/www/html/ferdy/jungledragon/index.php [L]
RewriteCond $1 !^(index\.php|images|img|css|js|swf|type|themes|robots\.txt|favicon\.ico|sitemap\.xml)
RewriteRule ^(.*)$ /var/www/html/ferdy/jungledragon/index.php/$1 [L]
Options +FollowSymlinks
RewriteEngine on

RewriteRule ^$ /var/www/html/ferdy/jungledragon/index.php [L]
RewriteCond $1 !^(index\.php|images|img|css|js|swf|type|themes|robots\.txt|favicon\.ico|sitemap\.xml)
RewriteRule ^(.*)$ /var/www/html/ferdy/jungledragon/index.php/$1 [L]

rewritecond %{http_host} ^jungledragon.com [nc]
rewriterule ^(.*)$ http://www.jungledragon.com/$1 [r=301,nc]
这些规则对于CI应用程序来说是相当标准的。它们将所有URL(例外列表中的URL除外)重写到index.php前端控制器。上面的行还隐藏index.php,因为它通常会显示为每个URL的一部分

到目前为止,一切顺利。一切正常。现在,为了搜索引擎优化,我想强制所有流量访问www。因此,我将规则扩展如下:

RewriteRule ^$ /var/www/html/ferdy/jungledragon/index.php [L]
RewriteCond $1 !^(index\.php|images|img|css|js|swf|type|themes|robots\.txt|favicon\.ico|sitemap\.xml)
RewriteRule ^(.*)$ /var/www/html/ferdy/jungledragon/index.php/$1 [L]
Options +FollowSymlinks
RewriteEngine on

RewriteRule ^$ /var/www/html/ferdy/jungledragon/index.php [L]
RewriteCond $1 !^(index\.php|images|img|css|js|swf|type|themes|robots\.txt|favicon\.ico|sitemap\.xml)
RewriteRule ^(.*)$ /var/www/html/ferdy/jungledragon/index.php/$1 [L]

rewritecond %{http_host} ^jungledragon.com [nc]
rewriterule ^(.*)$ http://www.jungledragon.com/$1 [r=301,nc]
最后两行将URL重写为URL。这种方法是可行的,但它将index.php部分带回来:变成

我如何准确地组合这些规则,使它们不会相互干扰?我试着在CI规则之前重写WWW。这显示了一个带有错误的apache301页面,而不是执行实际的重定向

此外,我还想包括一些规则,以消除尾部斜杠,但现在让我们保持问题的简单性。请注意,我确实在这里和其他地方找到了有用的帖子,但出于某种原因,我仍然无法找到适合我的情况的正确语法

编辑:谢谢您的帮助。这项工作:

Options +FollowSymlinks
RewriteEngine on

rewritecond %{http_host} ^jungledragon.com [nc]
rewriterule ^(.*)$ http://www.jungledragon.com/$1 [r=301,nc,L]

RewriteRule ^$ /var/www/html/ferdy/jungledragon/index.php [L]
RewriteCond $1 !^(index\.php|images|img|css|js|swf|type|themes|robots\.txt|favicon\.ico|sitemap\.xml)
RewriteRule ^(.*)$ /var/www/html/ferdy/jungledragon/index.php/$1 [L]

mod_rewrite
以线性方式处理规则。首先处理文件顶部的规则

规则末尾的
[nc]
[L]
是如何处理规则的选项

  • nc-nocase:大小写不重要
  • L-last:执行中的最后一条规则(如果点击此按钮,则停止处理)
您需要将
www重定向
规则置于CI规则之上,这样它将首先添加www,然后将CI规则应用于新重新编写的url**还可以对
www-redirect
规则使用
C
N
标志,以便解析下一条规则

http://mysite.com/blah
==变成==>
http://www.mysite.com/blah
==变成==>
http://www.mysite.com/index.php/blah
(已执行,未重定向)

目前的情况是:

http://mysite.com/blah
==变成==>
http://mysite.com/index.php/blah
(停止)

浏览器转到
http://mysite.com/index.php/blah
第二次重写过程完成,因为您的异常停止了/index.php URL的处理

http://mysite.com/index.php/blah
==变成==>
http://www.mysite.com/index.php/blah
(重定向)


正如所建议的,如果您想进一步了解,这里有一个指向的链接

@懒鬼:笨蛋,对不起

以下是文档摘录,概述了您可能需要的标志:

'chain | C'(用下一条规则链接) 此标志将当前规则与下一个规则链接(下一个规则本身可以与以下规则链接,依此类推)。这具有以下效果:如果规则匹配,则处理照常继续-该标志无效。如果规则不匹配,则跳过以下所有链接规则。例如,当允许发生外部重定向时(不应发生.www''部分!),它可用于删除每个目录规则集中的
.www''部分

下一轮(下一轮) 重新运行重写过程(使用第一条重写规则重新开始)。这一次,要匹配的URL不再是原始URL,而是上次重写规则返回的URL。这与C中的Perl next命令或continue命令相对应。使用此标志重新启动重写过程-立即转到循环的顶部。 小心不要创建无限循环

'nocase | NC'(无案例) 这使得模式不区分大小写,当模式与当前URL匹配时,忽略“A-Z”和“A-Z”之间的差异。 'noescape | NE'(输出无URI转义) 此标志防止mod_rewrite将常用的URI转义规则应用于重写的结果。通常,特殊字符(如“%”、“$”、“;”等)将转义为其等价的十六进制代码(分别为“%25”、“24”和“%3B”);此标志可防止发生这种情况。这允许百分比符号出现在输出中,如中所示


OPs规则中没有[C]标志,只有[NC]。你也可以发布一个Apache的mor_重写手册的链接,这样OP可以自己学习更多。很抱歉还没有回复,我正在工作,今晚会尝试这个。