URL重写htaccess以及PHP [英] url rewrite htaccess and php

查看:204
本文介绍了URL重写htaccess以及PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要重写URL,并为我有以下的code。

I want to rewrite URL and for that I have got following code.

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

现在,当我写的UR​​L例如 www.mywebsite.com/username 它工作正常,并没有给出任何错误。

now when I write url like www.mywebsite.com/username it works fine and not giving any error.

现在我怎样才能得到,即使我写的 www.mywebsite.com/index.php?uname=username 在击去这个网址。

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 www.mywebsite.com/username 当我写的 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.

至于$ P $ WWW的PFIX重写工作,我想同样的方法来改变我的网址为 www.mywebsite.com/username ,即使用户的写www.mywebsite.com/index的.php?的uname =用户名

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.

推荐答案

以下应该工作:

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

这将检查文件或目录不存在,如果它不将假设它是一个用户名和重写URL。

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.

如果您想从的index.php重定向?的uname =用户名 /用户名那么你可以添加此到顶部的的index.php

If you want to redirect from index.php?uname=username to /username then you could add this to the top of your index.php:

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

这篇关于URL重写htaccess以及PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆