htaccess的mod_rewrite的URL与多个可选参数多个、可选、参数、htaccess

2023-09-02 20:34:17 作者:孤煞

我是pretty的新的这个mod_rewrite的业务,但我想有一个规则,让我完成以下任务:

I'm pretty new to this mod_rewrite business but I'd like to have a rule that allows me to accomplish the following:

localhost/module_name/ -> localhost/index.php?module=module_name
localhost/module_name/module_action -> localhost/index.php?module=module_name&action=module_action
localhost/module_name/module_action/parm1 -> localhost/index.php?module=module_name&action=module_action&parm_1=parm1
localhost/module_name/module_action/parm1/parm2 -> localhost/index.php?module=module_name&action=module_action&parm_1=parm1&parm_2=parm2

等。我设法MODULE_NAME和module_action工作,但我不知道如何得到它只有一个模块或多个参数来工作。这是我目前有:

and so on. I managed to get module_name and module_action to work, but I can't figure out how get it to work with only a module or with multiple parameters. This is what I currently have:

RewriteEngine on
RewriteRule ([a-zA-Z]+)/([a-zA-Z]+) index.php?module=$1&action=$2
RewriteRule ([a-zA-Z]+)/([a-zA-Z]+)/([a-zA-Z]+)$ index.php?module=$1&action=$2&parm=$3

第一条规则似乎工作,但它打破分开的第二个。

The first rule seems to work but it breaks apart on the second one.

任何帮助将是非常美联社preciated。

Any help would be really appreciated.

推荐答案

您可以尝试这样的:

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI}  ^/([^/]+)/?([^/]*)?/?([^/]*)?/?([^/]*)?/?   [NC]
RewriteRule .*    index.php?key1=%1&key2=%2&key3=%3&key4=%4  [L]

地图默默

的http://本地主机/ VAL1 /

的http://本地主机/ VAL1 / val2次/ VAL3 / VAL4

要:

的http://localhost/index.php键1 = VAL1

的http://localhost/index.php键1 = VAL1和放大器;键2 = val2的&放大器; KEY3 = VAL3和放大器; KEY4 = VAL4

不传入 VALN 值是空的替换URL。

Not incoming valN values are empty in the substitution URL.

的index.php 被认为是一个固定的字符串。

index.php is considered a fixed string.

有关永久重定向,用替换[L] [R = 301,L]

参数的最大数目= 4。

Maximum number of parameters = 4.