Pretty URLs with .htaccess

2019-01-06 22:37发布

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.

8条回答
对你真心纯属浪费
2楼-- · 2019-01-06 22:45

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
查看更多
Melony?
3楼-- · 2019-01-06 22:51

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

查看更多
\"骚年 ilove
4楼-- · 2019-01-06 22:52

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

查看更多
叛逆
5楼-- · 2019-01-06 22:54

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.

查看更多
干净又极端
6楼-- · 2019-01-06 22:59

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

查看更多
我只想做你的唯一
7楼-- · 2019-01-06 23:02

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.

查看更多
登录 后发表回答