Pretty URLs with .htaccess
Asked Answered
M

9

14

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.

Maxilliped answered 1/8, 2014 at 12:58 Comment(4)
The basics, 24ways.org/2013/url-rewriting-for-the-fearfulEldin
Are all your users identified by letters or numbers? Or both? E.g. /user/1, /user/BigChris, /user/BigChris1?Moriarty
@BigChris numbers onlyMaxilliped
Htaccess tutorial for beginners helponnet.com/2021/04/15/htaccess-tutorial-for-beginersRheinland
M
1

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.

Maxilliped answered 1/8, 2014 at 13:47 Comment(0)
P
11

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.

Priedieu answered 1/8, 2014 at 13:15 Comment(2)
Thanks, but how about the user = empty? Example localhost/index.php?user&action=add. How can I make it into localhost/user/add ?Maxilliped
When you do this rewrite - and perhaps the site has been live for a long time, are there things you should consider? that example.com/news/slug-of-title is the same as example.com/index.php?action=news&slug=slug-of-title ?Prophase
M
7

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

Macbeth answered 1/8, 2014 at 13:15 Comment(2)
expload has a nice ring to it.Fairyfairyland
It is explode not exploadTarpan
S
6

you can write something like this:

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /index.php?user=$1&action=$2 [L]
Savage answered 1/8, 2014 at 13:12 Comment(1)
I'm aware this is a rather old answer. but just wanted it made aware for any that come back here, this link is long dead.Charming
C
4

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.

Caporetto answered 1/8, 2014 at 13:24 Comment(0)
M
3

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

Moriarty answered 1/8, 2014 at 13:8 Comment(2)
Thanks. I am going to try this.Maxilliped
Ahhh damnit! I completely forgot the square brackets!Moriarty
V
1

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 !!

Vanillic answered 1/8, 2014 at 13:37 Comment(0)
M
1

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.

Maxilliped answered 1/8, 2014 at 13:47 Comment(0)
P
0

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
Pavid answered 18/4, 2016 at 11:28 Comment(0)
R
-1

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!

Rab answered 17/8, 2024 at 20:13 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.