我可以使用哪些代码与htaccess建立平面链接? 我想要的一个例子:
site.com/folderA成为:site.com/index.php?section=folderA
site.com/folderA/folderB成为:site.com/index.php?section=folderA&action=folderB
site.com/folderA/folderB/folderC成为:site.com/index.php?section=folderA&action=folderB&id=folderC
我尝试使用以下代码:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([^/]+) index.php?section=$1 [NC]
RewriteRule ^([^/]+)/([^/]+) index.php?section=$1&action=$2 [NC]
RewriteRule ^([^/]+)/([^/]+)/([^/]+) index.php?section=$1&action=$2&id=$3 [NC]
答案 0 :(得分:1)
请务必使用$
(行尾锚点)以避免匹配不需要的网址:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# if request is not for a file/directory
RewriteCond %{SCRIPT_FILENAME} -d [OR]
RewriteCond %{SCRIPT_FILENAME} -f
# then skip from rewrites
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/?$ index.php?section=$1 [NC,L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?section=$1action=$2 [NC,L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?section=$1action=$2id=$3 [NC,L,QSA]