【问题标题】:RewriteCond to skip rule if file or directory exists如果文件或目录存在,则 RewriteCond 跳过规则
【发布时间】:2012-04-19 07:38:56
【问题描述】:

如果文件或目录存在,我正在尝试调整我的 htacess 文件以跳过重写规则,但是当文件存在时,它会将 URL 更改为不同的内容。我不知道为什么会这样。这是我的 htaccess 文件:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^project/([^/\.]+)/?$ index.php?view=project&project=$1 [L]
RewriteRule ^for/content/ag index.php?view=opening&section=ag [L]
RewriteRule ^for/content/bus index.php?view=opening&section=bus [L]
RewriteRule ^for/content/fcs index.php?view=opening&section=fcs [L]
RewriteRule ^for/content/market index.php?view=opening&section=market [L]
RewriteRule ^for/content/trade index.php?view=opening&section=trade [L]
RewriteRule ^for/content/teched index.php?view=opening&section=teched [L]
RewriteRule ^for/content/([^/\.]+)/?$ index.php?view=content_area&section=$1 [L]

目录 /project/nti 存在,带有一个 index.php 文件。当输入地址 mywebsite.com/folder/project/nti 时,它会更改为 mywebsite.com/folder/project/nti/?view=project&project=nti。我将它应用在示例站点位于子文件夹中的测试服务器上,但实施后,它将位于根 Web 服务器文件夹中。

感谢您的帮助!

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    删除多余的[OR] 并让您的代码如下:

    # If the request is not for a valid directory
    RewriteCond %{REQUEST_FILENAME} !-d
    # If the request is not for a valid file
    RewriteCond %{REQUEST_FILENAME} !-f
    # If the request is not for a valid link
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^project/([^/.]+)/?$ index.php?view=project&project=$1 [L,NC,QSA]
    

    【讨论】:

    • 为此添加一点解释:OR 表示如果文件名是“不是文件”或“不是目录”,因为某些东西永远不会 both文件和目录,这将永远成立......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 2012-03-23
    • 1970-01-01
    • 1970-01-01
    • 2017-02-19
    • 1970-01-01
    • 2020-05-23
    相关资源
    最近更新 更多