Apache mod_重写的复杂规则
请帮助我创建正确的修改说明。这似乎很难 我需要的正是: 一,。从www.site.com重定向到site.com 等等,然后应用其他规则 二,。直接访问某些指定的文件和文件夹,如robots.txt、images/等,无需重写 也许吧 我说得对吗? 所以,URL像Apache mod_重写的复杂规则,apache,mod-rewrite,Apache,Mod Rewrite,请帮助我创建正确的修改说明。这似乎很难 我需要的正是: 一,。从www.site.com重定向到site.com 等等,然后应用其他规则 二,。直接访问某些指定的文件和文件夹,如robots.txt、images/等,无需重写 也许吧 我说得对吗? 所以,URL像 site.com/robots.txt ==> site.com/robots.txt site.com/documents/file1.pdf ==> site.co
site.com/robots.txt ==> site.com/robots.txt
site.com/documents/file1.pdf ==> site.com/documents/file1.pdf
应该保持原样,不应该重写
三,。另一个转变:
site.com/index.php?anythinghere ==> site.com/a/index.php?anythinghere
四,。但是如果我们键入URL site.com和site.com/Apache,那么应该在根文件夹site.com/index.php中调用index.php
site.com ==> site.com/index.php
site.com/ ==> site.com/index.php
五,。我有MVC脚本,如果我们键入以下URL:
1. site.com/controller or site.com/controller/ or site.com/controller//
2. site.com/controller/function or site.com/controller/function/
3. site.com/controller/function/parameter or site.com/controller/function/param1/param2/param3
如果controller是列表中预定义的单词之一,比如index、news、contacts,它可能会扩展到几百个单词,那么我们应该调用index.php,按照以下方式重写URL:
site.com/controller ==> site.com/index.php?q=controller
site.com/controller/ ==> site.com/index.php?q=controller/
site.com/controller// ==> site.com/index.php?q=controller//
site.com/controller/function ==> site.com/index.php?q=controller/function
site.com/controller/function/ ==> site.com/index.php?q=controller/function/
site.com/controller/function/parameter ==> site.com/index.php?q=controller/function/parameter
site.com/controller/function/param1/param2/param3 ==> site.com/index.php?q=controller/function/param1/param2/param3
六,。最后,如果之前的所有规则都不适用,我们应该重写
site.com/anythinghere ==> site.com/a/index.php?anythinghere
希望apache不要递归地使用mod_rewrite,否则使用不同的index.php会有很大的麻烦
我知道这并不容易,但如果你能帮助为一个项目创建规则,那就太好了
提前谢谢 对于第一次重写,您可以将以下行放入.htaccess:
RewriteCond %{HTTP_HOST} ^www.site.com$ [NC]
RewriteRule ^(.*)$ http://site.com/$1 [R=301]
将R=301设置为永久重定向非常重要,这样搜索引擎就不会将www.site.com和site.com视为两个不同的站点
对于第二次重写,有许多选项可用。让apache为所有实际存在的文件(如图像和pdf文档)提供服务,并重写那些意味着一些MVC命令的URL,可能对您更有用
如果你这么认为,你可以覆盖你的第五次重写,并写下以下内容:
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^/news$ ./index.php?news [L]
RewriteRule ^/news/(.*)$ ./index.php?news/$1 [L]
对于您拥有的每个控制器,依此类推。如果您计划拥有数百个控制器,那么我认为您应该重新考虑系统的设计,或者让它们成为几个主控制器的子控制器,这样这些主控制器就可以写在这个.htaccess文件中
同样重要的是L,它告诉apache如果匹配条件,它是最后一行执行的最后一行,所以如果我记得清楚的话,可以避免递归
关于第三个问题:
要介绍第六个也是最后一个:
RewriteRule ^/(.*)$ ./a/index.php?$1 [L]
因此,涵盖重写1、2、3、5和6,我们可以得到:
RewriteEngine On
# First rewrite
RewriteCond %{HTTP_HOST} ^www.site.com$ [NC]
RewriteRule ^(.*)$ http://site.com/$1 [R=301]
# Second and fifth rewrite
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^/news$ ./index.php?news [L]
RewriteRule ^/news/(.*)$ ./index.php?news/$1 [L]
# Third rewrite
RewriteRule ^/index.php?(.*)$ ./a/index.php?$1 [L]
# Sixth rewrite
RewriteRule ^/(.*)$ ./a/index.php?$1 [L]
我对你的问题做了一些编辑,以便更清楚地说明这些转换。希望有帮助。我现在无法访问我的开发服务器,所以我还不能测试这些规则。
RewriteRule ^/index.php?(.*)$ ./a/index.php?$1 [L]
RewriteRule ^/(.*)$ ./a/index.php?$1 [L]
RewriteEngine On
# First rewrite
RewriteCond %{HTTP_HOST} ^www.site.com$ [NC]
RewriteRule ^(.*)$ http://site.com/$1 [R=301]
# Second and fifth rewrite
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^/news$ ./index.php?news [L]
RewriteRule ^/news/(.*)$ ./index.php?news/$1 [L]
# Third rewrite
RewriteRule ^/index.php?(.*)$ ./a/index.php?$1 [L]
# Sixth rewrite
RewriteRule ^/(.*)$ ./a/index.php?$1 [L]