【发布时间】:2010-12-18 05:00:56
【问题描述】:
一两天后,我仍在与 Mod Rewrite 战斗。看来我 60% 的开发时间都花在了与服务器的战斗上。
我当前的 URL 结构是所有http://example.com/xyz 和http://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 中的任何缺陷吗?
【问题讨论】:
-
总结我的问题,我无法访问服务器上存在的目录,因为 mod rewrite 正在重定向我。这不应该发生,因为我设置了 RewriteCond %{REQUEST_FILENAME} !-d
-
要尝试的问题,将倒数第二个或最后一个规则从 ...php?n=... 更改为 ...phph?m=... 以查看哪些规则正在实际应用。另外你说它是如何重定向到 admin/?n=admin 的,但是你指的是 index.php 获取 n=admin 参数
标签: regex .htaccess mod-rewrite lamp