简体   繁体   中英

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 )

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

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

Then in PHP you could access it just using $_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. 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

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

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.

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' ];

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.

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

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

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.

Oop php front controller issue

Make sense.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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