【发布时间】:2009-08-20 21:56:40
【问题描述】:
我正在尝试将 URL 从 example.net/customname 或 example.net/customname/ 重定向到 example.net/my/home.php?username=customname 。这本身并不复杂:
RewriteRule ^([a-zA-Z0-9_-]+)$ my/home.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ my/home.php?username=$1
但是,我想排除我现有的目录和文件,例如 example.net/about/ 和 example.net/files。我不太清楚如何使用 RewriteCond(我需要多个重写条件吗?)以排除像 example.net/about/ 这样的项目被重写为 example.net/home.php?username=about。我该怎么做?
谢谢!
编辑:这是确切的解决方案...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/?$ my/home.php?url=$1 [NC,L]
【问题讨论】:
-
关于您的编辑:为什么您使用两组完全独立的规则,其唯一区别是单个字符?它冗长且效率低下,所以我建议使用选项限定符(问号)作为 localshred,我在我们的答案中也这样做了。
-
很对,我把答案改成了reflect。
标签: regex apache mod-rewrite url-rewriting