.htaccess - How to change the name of the directory in url in php using htaccess? -
how can change name of url of site is: www.example.com/user/panel.php www.example.com/username/panel.php "username" unique each user , , each login name of user database, in jsfiddle.net, me?
personally not use .htaccess ( )
that said time people way
rewriteengine on rewriterule ^users/([-a-za-z0-9_]+)/?$ index.php?user=$1 [l] so if had url like
www.yoursite.com/users/someguy then pass apache ( , php )
www.yoursite.com/index.php?user=someguy then in php access using $_get[user].
now ignoring security concerns may have ( shouldn't rely on user input tell are, can lie it) use call uri method ( not url ) uri imaginary path. method employed many mvc systems. start uri
www.yoursite.com/index.php/users/someguy notice index.php ( in middle ). .htaccess this
rewriteengine on rewritecond %{request_filename} !-f #if not real file rewritecond %{request_filename} !-d #if not real folder rewriterule ^(.*)$ index.php/$1 [l] #hide index.php so allow remove index.php giving url this
www.yoursite.com/users/someguy which want, , looks same first case.
then cam use $_server['query_string'] supper global give past index.php
/users/someguy and can split up, route somewhere, whatever need it. this
$uri = array_filter( explode('/', $_server['query_string'] ) ); //$uri = [ 'users', 'someguy' ]; now reason more, it's more flexible , lets use query string ?var part of url other stuff. ( bookmarkable search forms ) ie. feels less hacky because not breaking query parameters of request. conversely, first method, if .htaccess sloppy make query part of url unusable on site, , feels wrong me.
it easier maintain, because requires no further setup additional pretty urls
for example: want prettyfy product. using first method have go .htaccess add @ 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 more complex levels if have product categories
rewriterule ^produts/([-a-za-z0-9_]+)/(0-9_]+)/?$ index.php?category=$1&product_id=$2 [l] after wile wind dozens of rules in there, of may not clear do. realize spelled products produts , have start renaming things. it's mess later on.
now using second method don't need additional steps, besides routing in index page. put url in
www.yoursite.com/products/123 and pull stuff $_server array no further messing rewrite rules.
here previous answer did outlines how build basic router.
oop php front controller issue
make sense.
Comments
Post a Comment