如何使用htaccess在php中的url中更改目录的名称? [英] How to change the name of the directory in url in php using htaccess?

查看:90
本文介绍了如何使用htaccess在php中的url中更改目录的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将我的网站的网址名称更改为:www.example.com/user/panel.php更改为www.example.com/username/panel.php,其中用户名是唯一的对于每个用户,对于每个登录名,都是jsfiddle.net中数据库中用户的名称,他们可以帮助我吗?

How can I do to change the name of the url of my site which is: www.example.com/user/panel.php to www.example.com/username/panel.php where the "username" is unique for each user , And for each login would be the name of the user from database, as it is in jsfiddle.net, could they help me?

推荐答案

我个人不会使用.htaccess(特别是)

Personally I would not use .htaccess for this ( specifically )

大多数人说这种方式的时候都会使用它。

that said most the time people do it this way

RewriteEngine On
RewriteRule ^users/([-a-zA-Z0-9_]+)/?$ index.php?user=$1 [L]

因此,如果您有类似

 www.yoursite.com/users/someguy

然后将其作为

www.yoursite.com/index.php?user=someguy

然后在PHP中,您可以使用 $ _ GET [user]

Then in PHP you could access it just using $_GET[user].

现在忽略安全隐患,我可能会为此(您不应该依赖用户输入来告诉他们他们是谁,他们可以撒谎)为此,我将使用所谓的URI方法(不是URL),URI是一个虚构的路径。这也是许多MVC系统采用的方法。因此,我将从URI

Now ignoring security concerns I may have ( you shouldn't rely on user input to tell who they are, they can lie about it) for this I would use what I call the URI method ( not URL ) a URI is an imaginary path. This is also the method employed by many MVC systems. So for this I will start with the URI

 www.yoursite.com/index.php/users/someguy

注意index.php的位置(在中间)。然后,您可以像这样进行.htaccess

Notice where the index.php is ( in the middle ). Then you do a .htaccess like this

  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-f  #if not a real file
  RewriteCond %{REQUEST_FILENAME} !-d  #if not a real folder
  RewriteRule ^(.*)$ index.php/$1 [L] #hide the index.php

所以这样做是允许您删除 index.php 给您这样的网址

So what this does is allow you to remove the index.php giving you a url like this

  www.yoursite.com/users/someguy

我们想要的是,并且看上去与第一种情况基本相同。

Which is what we want, and looks basically the same as the first case.

然后您可以使用 $ _ SERVER ['QUERY_STRING'] 全局晚餐,它将提供index.php之后的所有内容。

Then you cam use the $_SERVER['QUERY_STRING'] supper global which will give you everything past index.php

   /users/someguy

您可以将其拆分,路由将其放置在某处,随便您做什么。像这样

And you can split that up, route it somewhere, do whatever you need to with it. Like this

 $uri = array_filter( explode('/', $_SERVER['QUERY_STRING'] ) );

 //$uri = [ 'users', 'someguy' ];

现在我之所以更喜欢它,是因为它更灵活,它可以让您使用查询字符串url的?var 部分用于其他内容。 (例如可收藏书签的搜索表单),即因为您没有破坏GET Request的查询参数,所以感觉不太hack。相反,使用第一种方法,如果您的.htaccess太松散,则可以使它成为URL的查询部分在您的网站上不可用,这对我来说是错的。

Now the reason I like this more, is it's more flexible and it lets you use the query string the ?var part of the url for other stuff. ( like bookmarkable search forms ) ie. it feels less hacky because your not breaking the query parameters of a GET Request. Conversely, with the first method, if your .htaccess is sloppy you could make it were the query part of the URL is unusable on your site, and that just feels wrong to me.

它也更易于维护,因为它不需要为其他漂亮的url进行进一步的设置

It also easier to maintain, because it requires no further setup for additional pretty urls

例如:
假设您要漂亮的产品。使用第一种方法,您必须返回到 .htaccess ,至少还要添加一条规则:

For example: Say you want prettyfy your product. Using the first method you would have to go back to the .htaccess add at least 1 more rule in:

RewriteEngine On
RewriteRule ^users/([-a-zA-Z0-9_]+)/?$ index.php?user=$1 [L]
RewriteRule ^products/(0-9_]+)/?$ index.php?product=$1 [L]

如果您拥有产品类别,甚至可能更复杂的级别

Possibly even more complex levels if you have product categories

RewriteRule ^produts/([-a-zA-Z0-9_]+)/(0-9_]+)/?$ index.php?category=$1&product_id=$2 [L]

在花言巧语之后,您会发现其中有数十条规则,其中一些规则可能尚不清楚。然后,您意识到您将产品拼写为产品,并且不得不开始重命名。

After a wile you would wind up with dozens of rules in there, some of which may not be immediately clear as to what they do. Then you realize you spelled products as produts and have to start renaming things. It's just a mess later on.

现在,使用第二种方法,除了在索引页面中进行路由之外,您无需执行任何其他步骤。您只需将网址放入

Now using the second method you don't need to do any additional steps, besides routing it in your index page. You just put the url in

  www.yoursite.com/products/123

并从 $ _ SERVER 数组中删除这些内容,而不会进一步破坏重写规则。

And pull that stuff from the $_SERVER array with no further messing with rewrite rules.

这是我以前做过的答案,概述了如何构建基本路由器。

Here is a previous answer I did that outlines how to build a basic router.

糟糕php前端控制器问题

有道理。

这篇关于如何使用htaccess在php中的url中更改目录的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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