我正在尝试从我的域中删除PHP Get查询。例如,与其显示example.com/?url=1234
,我希望它重写为example.com/1234
,隐藏查询但不删除它。我知道这是可能的,并且已经阅读了许多有关如何执行此操作的教程,但是我的代码不起作用。这是我目前正在尝试的:
RewriteEngine On
RewriteCond %{QUERY_STRING} url=
RewriteRule (.*) http://example.com/$1? [R=301]
这样做是完全剥离查询,而不仅仅是删除?url=
段。
你考虑
的方式是错误的。重写不是客户端看到的东西,而是服务器独有的东西。
这意味着您可以使 example.com/1234 工作,就好像客户端使用了 example.com?url=1234 一样。
为此,您将使用以下行:
RewriteEngine On
RewriteRule (.*) http://example.com/url=$1 [QSA]
检查这个 - htaccess 重写查询字符串
和Htaccess 查询字符串重写
您需要在
RewriteCond
行中提取查询字符串的相关部分。
RewriteEngine On
RewriteCond %{QUERY_STRING} url=(.*)(&|$)
RewriteRule (.*) http://example.com/$1%1? [R=301]
上述内容将丢弃您提供的任何其他查询字符串参数(请参阅下面的示例)。如果给定,它还将保留文件名。
例子:
http://example.com/?url=1234 ----> http://example.com/1234
http://example.com/a/?url=1234 ----> http://example.com/a/1234
http://example.com/?url=1234&a=b ----> http://example.com/1234