【问题标题】:.htaccess rewrite query string not working.htaccess 重写查询字符串不起作用
【发布时间】:2015-08-15 03:12:56
【问题描述】:

我已经从 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]

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

【问题讨论】:

  • test/…之后没有正则表达式占位符

标签: php .htaccess


【解决方案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/ 开头的url,使用test/ 之后的所有内容作为名称参数"。

【讨论】:

    【解决方案2】:

    尝试使用 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
    

    这里是“。”是任何请求。

    【讨论】:

    • OP 已经在使用相对于根的替换,因此实际上不需要 RewriteBase
    猜你喜欢
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    • 2012-11-15
    • 2013-02-19
    相关资源
    最近更新 更多