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
.
Try this it is very simple:
For
/user/add
you will need to do a separate rule because you have no "middle parameter". So:You can then do additional rules for URLs that contain additional parameters:
This will allow you to perform actions on existing users. E.g.
/user/1/update
a simple way is to pass only one variabe to index.php like this
and in your index.php file you do this
this one works for all cases, when you try to pass many variables, it doesn't work in the wase when you do something like
the last variable always takes the value
add/12/54/54/66
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:
by using
var_dump($_GET);
on the linklocalhost/user/1234/update
I gotwhile
localhost/user/add
which is my goal. I will just only do other stuffs under the hood with PHP.
Its simple just try this out !
Thats it !!
If you want to turn
http://www.yourwebsite.com/index.php?user=1&action=update
into
http://www.yourwebsite.com/user/1/update
You could use
To see the parameters in PHP:
This is, in my opinion, a little bit more clean and secure than (.*) which basically mean almost anything is accepted.