【发布时间】:2017-06-29 21:42:24
【问题描述】:
首先,我想要实现的目标: 我在基础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 前添加斜杠只会让事情变得更糟。 我将感谢一位眼光敏锐的专家,他能找出我问题的根本原因。
【问题讨论】:
-
我不太明白你问题的第二部分。导致错误的示例 URL 是什么,您希望它重定向到什么路径,以及它实际重定向到什么路径?
-
示例 URL 为:example.com/subdir/action/about。单击时,它会重定向到正确的 About 页面,但是下一个请求的路径变为 example.com/subdir/action。因此,当您单击操作/条款链接时 - 生成的 url=example.com/subdir/action/action/terms。注意添加到路径中的额外“action”文件夹(我需要重定向到example.com/subdir/index.php?p=terms)。所有链接都是相对的 BTW,例如行动/关于。谢谢。
标签: php .htaccess mod-rewrite directory