Port 8080
Port 8080

Reputation: 888

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.

Upvotes: 14

Views: 41740

Answers (9)

Muhammad Ahmed
Muhammad Ahmed

Reputation: 39

To handle "sweet URLs" in PHP, you can use URL Parsing combined with .htaccess rules. Here's a straightforward method:

Instead of converting domain.com/index.php?user=1&action=update into domain.com/user/1/update, you can use the sweet URL format directly and parse it to handle actions.

For example, if you visit domain.com/user/1/, you can use this code in user.php to parse the URL:

$requestURI = $_SERVER['REQUEST_URI'];
$path = parse_url($requestURI, PHP_URL_PATH);
$components = array_filter(explode('/', trim($path, '/')));

This code splits the URL into components. To get the last component (like 1 in domain.com/user/1/), use:

$lastComponent = end($components);

To access any specific component, use $components[index], where index is the position you need.

Don't forget to add this .htaccess rule to handle the URL rewriting:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

For a more organized approach, check out my GitHub repository: sweet-url-handler.

Hope this helps!

Upvotes: -1

Khalid
Khalid

Reputation: 4798

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

Upvotes: 7

user1460340
user1460340

Reputation:

Its simple just try this out !

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^game/([0-9]+)/([_-0-9a-zA-Z]+)/?$  index.php?user=$1&action=$2 [L,NC]

Thats it !!

Upvotes: 1

stslavik
stslavik

Reputation: 3028

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.

Upvotes: 4

mogosselin
mogosselin

Reputation: 343

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.

Upvotes: 11

denoise
denoise

Reputation: 1097

you can write something like this:

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /index.php?user=$1&action=$2 [L]

Upvotes: 6

Hemal Halari
Hemal Halari

Reputation: 127

Try this it is very simple:

Options +FollowSymLinks

RewriteEngine on

RewriteRule index/user/(.*)/(.*)/ index.php?user=$1&action=$2

RewriteRule index/user/(.*)/(.*) index.php?user=$1&action=$2

Upvotes: 0

Kinnectus
Kinnectus

Reputation: 899

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

Upvotes: 3

Port 8080
Port 8080

Reputation: 888

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.

Upvotes: 1

Related Questions