0

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

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

当我尝试浏览到 CMS http://example.com/admin/时,它会将我的 URL 更改为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 中的任何缺陷吗?

4

2 回答 2

0

每个 RewriteCond 只应用下一个 RewriteRule。

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

您的选择是重复规则,或者最终确定并退出(我不记得这个标志了)

于 2009-11-19T08:06:32.760 回答
0

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

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

如果请求的路径可以映射到现有目录,这将结束重写过程。-f代替使用时同样适用。

于 2009-11-19T12:30:01.973 回答