.htaccess and rewrites Rules

2019-08-07 09:37发布

问题:

I have some difficulties to create a simply htaccess for url rewriting.

What I would like is url with 3 parameters :

www.example.com => index.php

www.example.com/module/ => index.php?module=$1

www.example.com/module/action/ => index.php?module=$1&action=$2

www.example.com/module/action/1 => index.php?module=$1&action=$2&params=$3

I did that with many tutorials, but when I try with the last one, it's failed

RewriteRule ^web/([^/]*)$ index.php?module=$1 [L,QSA]
RewriteRule ^web/([^/]*)/(.*[^/])$ index.php?module=$1&action=$2 [L,QSA]
RewriteRule ^web/([^/]*)/(.*[^/])/([0-9]*)$ index.php?module=$1&action=$2&params=$3 [L,QSA]

Someone could help me ?

Thanks

Bouffe

回答1:

The .* part of your second expression is gobbling up anything after /action/ so you need to limit it the same as the one right before the / and use the + instead of *:

RewriteRule ^web/([^/]+)$ index.php?module=$1 [L,QSA]
RewriteRule ^web/([^/]+)/([^/]+)/?$ index.php?module=$1&action=$2 [L,QSA]
RewriteRule ^web/([^/]+)/([^/]+)/([0-9]+)/?$ index.php?module=$1&action=$2&params=$3 [L,QSA]