【问题标题】:Rewrite rules confused重写规则混乱
【发布时间】:2010-10-30 12:27:59
【问题描述】:

我正在努力实现这个简单的事情......

我有一些静态页面应该是这样的
www.domain.com/profile 等。 问题是如何编写重写规则以便..

会有一些固定的重写 喜欢 /home

我希望每个存在的文件都不会被重写 www.domain.com/test.php 应该去 测试.php

最后,如果没有找到我希望它被重定向到 static.php?_.....

RewriteRule ^/home/?$ /index.php?__i18n_language=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/([^/]+)/?$ /static.php?__i18n_language=$1  

这工作正常,但如果我输入 index.php 或 test.php 甚至是来自其他重定向的 mach,它会让我进入 static.php... 请帮忙!

【问题讨论】:

    标签: mod-rewrite


    【解决方案1】:

    根据您的描述,您可以使用以下规则:

    # stop rewriting process if request can be mapped onto existing file
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteRule ^ - [L]
    
    # rewrite known paths /home, /foo, /bar, etc.
    RewriteRule ^/(home|foo|bar|…)$ /index.php [L]
    
    # rewrite any other path
    RewriteRule ^ /static.php [L]
    

    【讨论】:

    • 谢谢.. 问题是 request_filename 没有返回完整路径!通过添加前缀文档根变量来修复它
    • @Parhs:哦,是的,这是可能的:“匹配请求的文件或脚本的完整本地文件系统路径,如果在引用 REQUEST_FILENAME 时已经由服务器确定的话.否则,例如在虚拟主机上下文中使用时,与REQUEST_URI 的值相同。” (见RewriteCond directive
    【解决方案2】:

    我已经很久没有使用它了,但这是我发现的,应该会有所帮助。它是一个旧脚本的一部分,它生成 .httaccess 文件,以便仅在找不到文档时从 /usr/share/doc 重定向: 规则是“检查,如果目标url存在,则离开”:

    RewriteEngine On
    RewriteBase /doc/User_Documents
    
    ### If the directory already exists, then leave
    ### We're just redirecting request when the directory exists but has been renamed.
    
    RewriteCond %{REQUEST_FILENAME} User_Documents/([^/]+-[0-9][^/]*)
    RewriteCond $PWD/%1 -d
    RewriteRule .* - [L]
    

    [L] 表示如果满足其中一个条件则离开。在旧脚本中,有数百条生成的规则(在[L] 之后)被跳过,因为其中一个条件匹配。在您的情况下,当找到目标 %{REQUEST_FILENAME} 时,您将跳过其余规则。

    所以,我建议,在重定向规则之前:

    RewriteCond %{REQUEST_FILENAME} -f
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule .* - [L]
    

    【讨论】:

      猜你喜欢
      • 2017-03-13
      • 2011-02-19
      • 1970-01-01
      • 2020-07-09
      • 1970-01-01
      • 2010-11-24
      • 2011-11-01
      • 2020-09-28
      • 1970-01-01
      相关资源
      最近更新 更多