【问题标题】:Complicated rules for mod_rewritemod_rewrite 的复杂规则
【发布时间】:2012-05-13 10:41:24
【问题描述】:

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

我需要以下内容:

1。 重定向www.site.comsite.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会有很大的麻烦。

我知道这并不容易,但如果您能帮助为一件物品创建规则,那就太好了。

提前致谢!

【问题讨论】:

  • 我对您的问题进行了一些编辑,以便更清楚地进行转换。希望对您有所帮助。

标签: apache mod-rewrite


【解决方案1】:

对于第一次重写,您可以将这些行放在 .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-07-23
  • 2011-12-21
  • 1970-01-01
  • 2017-08-05
  • 2011-06-29
  • 2018-02-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多