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

Gan*_*alf 3 php regex apache .htaccess mod-rewrite

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

网址:

example.com/blah123/sys
Run Code Online (Sandbox Code Playgroud)

在 .htaccess 中,我有:

RewriteEngine On
RewriteRule ^([^/.]+)/?$ index.php?id=$1
Run Code Online (Sandbox Code Playgroud)

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

我尝试了以下方法,但它不起作用:

RewriteEngine On
RewriteRule ^([^/.]+)/?$/?$ index.php?id=$1?mode=$1
Run Code Online (Sandbox Code Playgroud)

我需要提取在第二个参数中传递的“模式”类型。因此,如果用户输入“example.com/blah123/sys”,我应该能够从 URL 中获取值“sys”。我怎样才能做到这一点?我想使用 PHP、MySql、.htacess。

更新:我目前的.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]
Run Code Online (Sandbox Code Playgroud)

anu*_*ava 5

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

这条规则应该有效:

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]
Run Code Online (Sandbox Code Playgroud)