Mod Rewrite忽略-d

时间:2009-11-19 07:46:04

标签: regex .htaccess mod-rewrite lamp

一两天后,我仍在与Mod Rewrite抗争。似乎60%的开发时间花在了与服务器的对抗上。

我当前的网址结构应该由index.php处理所有http://example.com/xyzhttp://example.com/xyz/abc。问题是我有一个http://example.com/admin/部分,它是一个真实目录,我需要通过HTTP请求访问它(它是CMS目录)

当我尝试浏览CMS http://example.com/admin/时,它会将我的网址更改为http://example.com/admin/?n=admin并返回404.我的index.php正在接收n = admin作为其参数。

我不明白为什么忽略这两个条件:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^admin(?:\/)?$

为什么我将重定向转移到http://example.com/admin/?n=admin(而非仅停留在http://example.com/admin/

Options +FollowSymlinks
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^admin(?:\/)?$

# allow access to certain subdirectories.
RewriteRule ^admin(?:\/)?$ /admin/ [L,NC]

# redirect all old URLs to new pages (or 404 sitemap page if no analog?).
RewriteRule ^company/about(?:\/)?$ /company [R=301,L,NC]

# catch any others and try to serve them right
RewriteRule ^/?(.+).html$ /$1 [R=301,L]

RewriteRule ^/?([0-9]+)(?:\/)?$ /index.php?p=$1 [L]
RewriteRule ^/?([-a-zA-Z0-9_+]+)(?:\/)?$ /index.php?n=$1 [L]
RewriteRule ^/?([-a-zA-Z0-9_+]+)/([-a-zA-Z0-9_+]+)(?:\/)?$ /index.php?n=$2 [L]

任何人都可以提供任何想法或指出.htaccess中的任何缺陷吗?

2 个答案:

答案 0 :(得分:0)

每个RewriteCond仅应用下一个RewriteRule。

所以只有你的第一条规则有条件。

你的选择是重复规则,或者做出最终和退出(不记得我的标志)

答案 1 :(得分:0)

在您的其他规则之前尝试此规则:

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

如果请求的路径可以映射到现有目录,这将结束重写过程。使用-f时也是如此。