首先,我想要实现的目标: 我正在基础http://example.com/subdir下进行测试,这是所有子文件夹,index.php和.htaccess的所在。
我有3组规则,如.htaccess所示(删除了额外的规则):
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
# Convert item/edit/xx -> index.php?p=edit&id=xx
# Convert item/remove/xx -> index.php?p=remove&id=xx
#RewriteCond %{REQUEST_URI} ^/item [NC]
#RewriteCond %{QUERY_STRING} ^id=([0-9]+)$ [NC]
RewriteRule ^item/([a-z]+)/([0-9]+)$ index.php?p=$1&id=$2 [NC,L]
# Convert category/yyyy -> customer/pagination.php?category=yyyy
#RewriteCond %{QUERY_STRING} category=([a-zA-Z\s]+)$
RewriteRule ^customer/category/([a-zA-Z\s]+)$ customer/pagination.php?category=$1 [NC,L]
# Convert action/about -> index.php?p=about
# Convert action/terms -> index.php?p=terms
#RewriteCond %{QUERY_STRING} p=([a-z]+)$
RewriteRule ^action/([a-z]+)$ index.php?p=$1 [NC,L]
</IfModule>
我遇到的第一个问题是RewriteCond无法正常工作(没有找到路径),所以它暂时被注释掉了。 RewriteRule在绝对路径下工作正常(例如RewriteRule ^ action /([az] +)$ http://example.com/subdir/index.php?p= $ 1 [NC,L]),但是这会导致浏览器显示真实的URL,因此我正在尝试使用它使用相对路径。 我的问题是,在第一次重定向后,左侧链接的第一部分会添加到路径中,即点击动作/关于链接后http://example.com/subdir变为http://example.com/subdir/action。 定义RewriteBase或在URL上添加斜杠只会让事情变得更糟。 我将感谢一位能够发现问题根源的鹰眼专家。
答案 0 :(得分:1)
第一个问题是%{REQUEST_URI}
总是包含完整路径。所以,你的病情可以改为:
RewriteCond %{REQUEST_URI} ^/subdir/item [NC]
使用.htaccess实际上无法解决第二个问题。您只需要告诉浏览器您想要什么。因此,您可以使用以下两种方法之一。
使用<base>
元素(适用于页面上所有相对URI):
<base href="/subdir" />
使用浏览器中显示的URL的相对路径:
<a href="terms">Click here</a>
如果您真的想使用.htaccess解决问题,唯一真正的方法是在请求后删除重复的目录。