Skip to content
Advertisement

Pretty URLs with .htaccess

I have a URL http://localhost/index.php?user=1. When I add this .htaccess file

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^user/(.*)$ ./index.php?user=$1

I will be now allowed to use http://localhost/user/1 link. But how about http://localhost/index.php?user=1&action=update how can I make it into http://localhost/user/1/update ?

Also how can I make this url http://localhost/user/add ? Thanks. Sorry I am relatively new to .htaccess.

Advertisement

Answer

Thanks for the idea @denoise and @mogosselin. Also with @stslavik for pointing out some of the drawback of my code example.

Here’s how I do it:

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^user/([0-9]*)/([a-z]*)$ ./index.php?user=$1&action=$2
RewriteRule ^user/([a-z]*)$ ./index.php?user&action=$1

by using var_dump($_GET); on the link localhost/user/1234/update I got

array (size=2)
  'user' => string '1234' (length=4)
  'action' => string 'update' (length=3)

while localhost/user/add

array (size=2)
  'user' => string '' (length=4)
  'action' => string 'update' (length=3)

which is my goal. I will just only do other stuffs under the hood with PHP.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement