How to create a friendly URL with two or more argu

2020-07-22 10:28发布

问题:

Mod rewrite novice here. I want to pass two URL parameters in the URL, but in a more friendlier format. If user passes, "example.com/blah123/sys", I should be able to extract the MySQL record "blah123" and the mode type "sys", in this case. Here's the example:

URL:

example.com/blah123/sys

In the .htaccess, I have:

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

The above works if the URL passed is: "example.com/blah123", but not "example.com/blah123/sys".

I tried the following, but it is not working:

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

I need to extract the "mode" type passed in the second argument. So, if the user enters "example.com/blah123/sys", I should be able to get the value "sys" from the URL. How can I do that? I want to use PHP, MySql, .htacess.

UPDATE: My current .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]

回答1:

Your regex is wrong. You cannot have $ following another $ in an input since $ denotes end of text.

This rule should work:

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]