如何使用mod-rewrite/htaccess创建一个带有两个或多个参数的友好URL



Mod在这里重写新手。我想在URL中传递两个URL参数,但格式更友好。如果用户通过了"example.com/blah123/sys",那么在这种情况下,我应该能够提取MySQL记录"blah123"和模式类型"sys"。下面是一个例子:

网址:

example.com/blah123/sys

在.htaccess中,我有:

RewriteEngine On
RewriteRule ^([^/.]+)/?$ index.php?id=$1

如果传递的URL是:"example.com/blah123",但不是"example.com/blah123/sys",则上述方法有效。

我尝试了以下操作,但不起作用:

RewriteEngine On
RewriteRule ^([^/.]+)/?$/?$ index.php?id=$1?mode=$1

我需要提取第二个参数中传递的"mode"类型。因此,如果用户输入"example.com/blah123/sys",我应该能够从URL中获得值"sys"。我该怎么做?我想使用PHP、MySql、.htaccess.

更新:我当前的.htaccess:

# Use PHP 5.3
AddType application/x-httpd-php53 .php 
RewriteEngine On
#RewriteRule ^([^/.]+)/?$ index.php?id=$1
RewriteRule ^([^/.]+)/([^/.]+)/?$ index.php?id=$1&mode=$2 [L,QSA]

您的正则表达式是错误的。输入中不能有$跟在另一个$后面,因为$表示文本结束。

这个规则应该有效:

RewriteEngine On
# new rule to handle example.com/blah123/sys
RewriteRule ^(w+)/(w+)/?$ /index.php?id=$1&mode=$2 [L,QSA]
# your existing rule to handle example.com/blah123
RewriteRule ^(w+)/?$ /index.php?id=$1 [L,QSA]

最新更新