我想重写URL,为此我有以下代码。
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?uname=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?uname=$1
现在,当我像 www.mywebsite.com/username 这样编写 url 时,它工作正常并且不会给出任何错误。
现在,即使我在 hit go 中写 www.mywebsite.com/index.php?uname=username,我如何才能获得此 URL。
我想在 URL 中写入 www.mywebsite.com/index.php?uname=username 时将 www.mywebsite.com/index.php?uname=username 更改为 www.mywebsite.com/username。
作为 WWW 前缀的重写工作,即使用户写 www.mywebsite.com/index.php?uname=username,我也想以同样的方式将我的 URL 更改为 www.mywebsite.com/username。
以下方法应该有效:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/'.]+)/?$ index.php?uname=$1
它将检查文件或目录是否不存在,如果不存在,它将假定它是一个用户名并重写URL。
如果要从index.php?uname=username
重定向到/username
,则可以将其添加到index.php
的顶部:
if (strpos($_SERVER['REQUEST_URI'], "uname") !== false)
{
header("Location: /" . $_GET['uname']);
}