.htaccess重写查询字符串不起作用

时间:2015-08-14 18:59:36

标签: php .htaccess

我已经搜索并尝试了Stackoverflow的所有可能的解决方案,但似乎没有任何东西适用于我的情况。

我正在尝试重写此网址:
http://www.example.com/test/name.php?name=somename

显示为
http://www.example.com/test/somename

这就是我目前在htaccess文件中的内容

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^test/?$ /test/name.php/?name=$1 [NC,L,QSA]

如果有人可以在这里提供一些帮助,我将不胜感激,因为我已被困在这一天超过一天。谢谢!

2 个答案:

答案 0 :(得分:1)

RewriteRule ^test/?$ /test/name.php/?name=$1 [NC,L,QSA]
                  ^---- "0 or 1 of the previous"

因为你在那里有^$个锚点,你基本上都在说

/test/
/test

只有两个可以匹配的URI。你可能想要

RewriteRule ^test/(.*)$ /test/name.php?name=$1

代替。 "以test/开头的网址,并使用test/之后的所有内容作为名称参数"。

答案 1 :(得分:0)

尝试使用RewriteBase!

RewriteEngine On
RewriteBase /test/

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . name.php?name=$1

在这里"。"是任何要求。