简体   繁体   English

如何使用htaccess在php中的url中更改目录的名称?

[英]How to change the name of the directory in url in php using htaccess?

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

Personally I would not use .htaccess for this ( specifically ) 我个人不会为此使用.htaccess(特别是)

that said most the time people do it this way 那说大多数时候人们都是这样

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

So if you had a url like 因此,如果您的网址像

 www.yoursite.com/users/someguy

Then it would pass it to apache ( and php ) as 然后将其传递给apache(和php)作为

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

Then in PHP you could access it just using $_GET[user] . 然后在PHP中,您可以只使用$_GET[user]来访问它。

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. 现在忽略我可能存在的安全性问题(您不应该依赖用户输入来告诉他们是谁,他们可以撒谎),为此,我将使用所谓的URI方法(不是URL),URI是一个假想的路径。 This is also the method employed by many MVC systems. 这也是许多MVC系统采用的方法。 So for this I will start with the URI 因此,我将从URI开始

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

Notice where the index.php is ( in the middle ). 注意index.php在哪里(在中间)。 Then you do a .htaccess like this 然后你像这样做一个.htaccess

  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

So what this does is allow you to remove the index.php giving you a url like this 因此,这样做是允许您删除index.php并为您提供这样的网址

  www.yoursite.com/users/someguy

Which is what we want, and looks basically the same as the first case. 这就是我们想要的,并且看起来与第一种情况基本相同。

Then you cam use the $_SERVER['QUERY_STRING'] supper global which will give you everything past index.php 然后,您可以使用$_SERVER['QUERY_STRING']全局晚餐,它将提供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' ];

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. 现在,我更喜欢它的原因是它更灵活,它使您可以将查询字符串的url的?var部分用作其他内容。 ( like bookmarkable search forms ) ie. (例如可收藏书签的搜索表单),即 it feels less hacky because your not breaking the query parameters of a GET Request. 因为您没有破坏GET Request的查询参数,所以感觉不太hack。 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. 相反,使用第一种方法,如果您的.htaccess太松散,则可以使它成为URL的查询部分在您的站点上不可用,这对我来说感觉不对。

It also easier to maintain, because it requires no further setup for additional pretty urls 它也更易于维护,因为它不需要为其他漂亮的URL进行进一步的设置

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: 使用第一种方法,您将不得不回到.htaccess ,再在其中添加至少一条规则:

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. 然后,您意识到您将products拼写为produts ,必须开始重命名。 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

And pull that stuff from the $_SERVER array with no further messing with rewrite rules. 并从$_SERVER数组中拉出这些东西,而不会进一步破坏重写规则。

Here is a previous answer I did that outlines how to build a basic router. 这是我以前做过的回答,概述了如何构建基本路由器。

Oop php front controller issue OOP PHP前端控制器问题

Make sense. 说得通。

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

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