Pretty URLs with .htaccess

2019-01-06 22:20发布

问题:

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.

回答1:

you can write something like this:

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


回答2:

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.



回答3:

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.



回答4:

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.



回答5:

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



回答6:

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 the wase when you do something like

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

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



回答7:

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



回答8:

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