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?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
Personally I would not use .htaccess for this ( specifically )
that said most the time people do it this way
So if you had a url like
Then it would pass it to apache ( and php ) as
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
Notice where the index.php is ( in the middle ). Then you do a .htaccess like this
So what this does is allow you to remove the
index.php
giving you a url like thisWhich 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.phpAnd you can split that up, route it somewhere, do whatever you need to with it. Like this
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:Possibly even more complex levels if you have product categories
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
asproduts
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
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.