1

I'm trying to remove a PHP Get query from my domain. For example, instead of showing example.com/?url=1234, I'd like it to rewrite to example.com/1234, hiding the query but not removing it. I know this is possible and have read many tutorials on how to do this, but my code just isn't working. Here's what I'm currently trying:

RewriteEngine On 
RewriteCond %{QUERY_STRING} url=  
RewriteRule (.*) http://example.com/$1? [R=301]

What this is doing is stripping the query entirely, instead of just removing the ?url= segment.

4

3 回答 3

0

您正在以错误的方式思考它。重写不是客户端看到的东西,而是服务器独有的东西。

这意味着您可以使 example.com/1234 像客户端使用 example.com?url=1234 一样工作。

为此,您将使用以下行:

RewriteEngine On   
RewriteRule (.*) http://example.com/url=$1 [QSA]
于 2012-04-12T09:01:19.363 回答
0

检查这个 - htaccess 重写查询字符串

Htaccess 查询字符串重写

于 2012-04-12T08:54:47.097 回答
0

您需要提取该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
于 2012-04-12T09:07:29.720 回答