.htaccess将查询字符串重写为具有多个目录的路径

时间:2016-01-05 07:41:23

标签: regex apache .htaccess mod-rewrite

我正在尝试对查询字符串进行非常简单的重写

http://www.example.com/directory1/directory2/test.php?id=8

http://www.example.com/directory1/directory2/test/8

我的代码是

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?test/(.*?)/?$ /test.php?id=$1 [L]

坚果它不工作我也尝试在stackoverflow上提供许多解决方案,但没有任何工作,请帮助我

1 个答案:

答案 0 :(得分:1)

将此规则放在/directory1/directory2/

Options -MultiViews
RewriteEngine On
RewriteBase /directory1/directory2/

RewriteCond %{THE_REQUEST} /test\.php\?id=([^\s&]+) [NC]
RewriteRule ^ test/%1? [R=302,L,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^test/(\w+)/?$ test.php?id=$1 [L,QSA,NC]