使用 htaccess 重写 url,隐藏查询字符串

例如我们有如下 URL:

http://example.com/users.php?name=tania

但是我们想要让 URL 变成如下:

http://example.com/users/tania

我们可以通过修改 .htaccess:

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

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /users\.php\?name=([^\&\ ]+)
RewriteRule ^/?users\.php$ /users/%1? [L,R=301]

我们仍然可以获取到 URL 查询参数.

users.php
<?php

echo $_SERVER['REQUEST_URI'] . '<br>';

print_r($_GET);
/users/tania Array ( [name] => tania )

猜你喜欢

转载自www.cnblogs.com/ryanzheng/p/9998873.html