Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i do readable urls?

Assume I have the url http://example.com/user/me, where me is the user name. When the user types the url into the address bar, I want to reveal the details of the user. I do not want urls such as http://example.com/user.php?user=me

Any help appreciated, working on LAMP


2 Answers

One way of doing this is using apache's module called mod_rewrite. You can then rewrite the urls for /user/([a-z]+) to point to /user.php?user=$1

Search for the documentation for mod_rewrite for the details.

like image 104
Laurent Avatar answered Sep 17 '26 22:09

Laurent


The most simple way would be (in .htaccess, in your server configuration or in your vhosts configuration):

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

This will forward all your requests that do not match a file, a directory or a symbolic link to your index.php. In there you can inspect the request URI to determine what to do. In fact you will need some kind of dispatcher that knows how to deconstruct the URL to determine what action should be taken. In fact this could result in some MVC implementation - but that's not a requirement.

like image 28
Stefan Gehrig Avatar answered Sep 17 '26 21:09

Stefan Gehrig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!