Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 247
Port 8080 Avatar asked Aug 01 '14 12:08

Port 8080


5 Answers

For /user/add you will need to do a separate rule because you have no "middle parameter". So:

RewriteRule ^user/add$ ./index.php?action=add [L,QSA]

You can then do additional rules for URLs that contain additional parameters:

RewriteRule ^user/([0-9]+)/([A-Za-z]+)$ ./index.php?user=$1&action=$2 [L,QSA]

This will allow you to perform actions on existing users. E.g. /user/1/update

like image 40
Kinnectus Avatar answered Oct 02 '22 17:10

Kinnectus


a simple way is to pass only one variabe to index.php like this

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^(.*)$ index.php?data=$1 [QSA]

and in your index.php file you do this

$data = expload("/",$_GET['data']);
$user = $data[1];
$action = $data[2];

this one works for all cases, when you try to pass many variables, it doesn't work in case you do something like this though

http://localhost/user/add/12/54/54/66

the last variable always takes the value add/12/54/54/66

like image 152
Khalid Avatar answered Oct 09 '22 05:10

Khalid


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

Options +FollowSymLinks
RewriteEngine On

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

To see the parameters in PHP:

<?php 
echo "user id:" . $_GET['user'];
echo "<br>action:" . $_GET['action'];
?>
  • The parenthesis in the .htaccess are groups that you can call later. with $1, $2, etc.
  • The first group I added ([0-9]*) means that it will get any numbers (1, 34, etc.).
  • The second group means any characters (a, abc, update, etc.).

This is, in my opinion, a little bit more clean and secure than (.*) which basically mean almost anything is accepted.

like image 10
mogosselin Avatar answered Oct 09 '22 03:10

mogosselin


you can write something like this:

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /index.php?user=$1&action=$2 [L]
like image 6
denoise Avatar answered Oct 09 '22 05:10

denoise


Since you tagged this with PHP, I'll add a little perspective from what I did, and it may or may not help you.

You can, of course, write solely in .htaccess, being careful about order. For instance, let's say that you have:

RewriteRule ^user/([0-9]+)/update$ ./index.php?user=$1&action=update
RewriteRule ^user/([0-9]+)$ ./index.php?user=$1

Then it should, upon receiving

http://localhost/user/1/update

go to

http://localhost/index.php?user=$1&action=update

and not

http://localhost/index.php?user=$1

Now, what I did instead was push everything to index.php?q=$1

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

Then I used index.php to handle how the query was broken up. So let's say someone enters

http://www.example.com/user/18239810/update

this would go to

http://www.example.com/index.php?q=user/18239810/update

From there, explode the query string along the first / to give user and 18239810/update.

This would tell me that I need to pass 18239810/update to the user controller. In that controller, I again explode the argument into the user id and command, and I can switch on the command to tell how to load the page, passing the user id as an argument to the update function.

Very quick and dirty example (index.php):

<?php
$getString = explode('/', $_GET['q'], 1);
$controller = $getString[0].'Controller';
require_once('/controllers/'.$controller.'.php');
$loadedController = new $controller( $getString[1] );
?>

Of course, this means that constructors all must take a string argument that will be parsed for acceptable values. You can do this with explodes and switch statements, always defaulting back to the standard front page to prevent unauthorized access based on random guessing.

like image 4
stslavik Avatar answered Oct 09 '22 05:10

stslavik