简体   繁体   English

url重写htaccess和php

[英]url rewrite htaccess and php

I want to rewrite URL and for that I have got following code. 我想重写URL,为此,我得到了以下代码。

    Options +FollowSymLinks
    RewriteEngine On
    RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?uname=$1
    RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?uname=$1

now when I write url like www.mywebsite.com/username it works fine and not giving any error. 现在,当我编写类似www.mywebsite.com/username的 url时,它可以正常工作,并且没有出现任何错误。

Now how can I get this URL even when I write www.mywebsite.com/index.php?uname=username in hit go. 现在,即使我一口气www.mywebsite.com/index.php?uname=username ,也如何获得该URL。

I want to change www.mywebsite.com/index.php?uname=username to www.mywebsite.com/username when I write www.mywebsite.com/index.php?uname=username in URL. 当我在URL中写入www.mywebsite.com/index.php?uname=username时,我想将www.mywebsite.com/index.php?uname=username更改为www.mywebsite.com/username

As rewrite work for prefix of WWW I want same way to change my URL to www.mywebsite.com/username even if user write www.mywebsite.com/index.php?uname=username . 至于WWW的前缀重写工作,我想同样的方式来改变我的网址www.mywebsite.com/username即使用户写入www.mywebsite.com/index.php?uname=username。

The following should work: 以下应该工作:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?uname=$1

It will check if the file or directory doesn't exist, and if it doesn't it will assume it's an username and rewrite the URL. 它将检查文件或目录是否不存在,如果不存在,则将假定它是用户名并重写URL。

If you want to redirect from index.php?uname=username to /username then you could add this to the top of your index.php : 如果要从index.php?uname=username重定向到/username则可以将其添加到index.php的顶部:

if (strpos($_SERVER['REQUEST_URI'], "uname") !== false)
{
    header("Location: /" . $_GET['uname']);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM