我有一个使用.htaccess进行URL路由的CI应用程序。我的基本设置如下:问题将CodeIgniter 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]
这些规则对CI应用程序来说非常标准。他们将所有的URL(例外列表中的除外)都重写到index.php前端控制器。上面的代码也会隐藏index.php,因为它通常会显示为每个URL的一部分。
到目前为止,这么好。一切正常。现在,为了搜索引擎优化,我想强制所有流量到www。所以,我伸出的规则如下:
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]
这最后两行重写http://jungledragon.com/anything网址http://www.jungledragon.com/anything网址。这种作品,但它带回了index.php部分:http://jungledragon.com/anything变成http://www.jungledragon.com/index.php/anything。
我究竟如何组合这些规则,以便它们不会相互干扰?我尝试在CI规则之前进行WWW重写。这显示了一个错误的Apache 301页面,而不是做实际的重定向。
此外,我还想包括规则来摆脱尾部斜杠,但现在让我们保持简单的问题。请注意,我在这里和其他地方找到了有用的帖子,但由于某些原因,我仍无法找到适合我的情况的正确语法。
编辑:感谢您的帮助。此作品:
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]
OP规则中没有[C]标志,只有[NC]。你也可以发布一个链接到Apache的mor_rewrite手册,这样OP可以学习更多的自己。 – LazyOne
对不起,没有回应,我在工作,但今晚会尝试这个。 – Ferdy
+1完美。谢谢 – brenjt