1

请帮助我为 mod_rewrite 创建正确的说明。这似乎非常困难。

我需要以下内容:

1.从www.site.com重定向site.com

www.site.com/hello                ==>     site.com/hello
www.site.com/abc/def              ==>     site.com/abc/def

等等,然后应该应用其他规则。

2、直接访问(不重写)一些指定的文件和文件夹(如robots.txt、images/等)

也许

RewriteCond %{REQUEST_URI} !^/(robots.txt|favicon.ico|images|documents|~.\*)

我对吗?所以,像这样的网址

site.com/robots.txt               ==>     site.com/robots.txt
site.com/documents/file1.pdf      ==>     site.com/documents/file1.pdf

应该保持原样,不应该被重写。

3.另一种转变:

site.com/index.php?anythinghere   ==>     site.com/a/index.php?anythinghere

4. 但是如果我们输入 URLs site.com 和 site.com/ Apache 应该调用根文件夹中的 index.php (site.com/index.php)

site.com                          ==>     site.com/index.php
site.com/                         ==>     site.com/index.php

5. 我有 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

如果控制器是列表中预定义的单词之一,比如“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

6. 最后,如果没有应用所有之前的规则,我们应该重写

site.com/anythinghere             ==>     site.com/a/index.php?anythinghere

希望apache不要递归地使用mod_rewrite,否则我会对不同的index.php有很大的麻烦。

我知道这并不容易,但如果你能帮助为一个项目创建规则,那就太好了。

提前致谢!

4

1 回答 1

0

对于第一次重写,您可以将这些行放在 .htaccess 中:

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

R=301为了使其成为永久重定向非常重要,这样搜索引擎就不会将www.site.comsite.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]

依此类推,controller您拥有的每一个。如果你打算拥有数百个控制器,那么我认为你应该重新考虑你的系统的设计,或者让它们成为几个主控制器的子控制器,这样这些主控制器就可以写在这个.htaccess文件中。

同样重要的是L,它告诉 apache 它是执行的最后一行(如果匹配条件,则为最后一行),因此您可以避免递归(如果我没记错的话,还有其他事情)。

覆盖第三个:

RewriteRule ^/index.php?(.*)$ ./a/index.php?$1  [L]

涵盖第六个和最后一个:

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]
于 2012-05-03T22:04:41.230 回答