douba8758 2013-11-29 02:23
浏览 49
已采纳

如何使用mod rewrite / htaccess创建一个包含两个或多个参数的友好URL?

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条回答 默认 最新

  • down00112 2013-11-29 02:26
    关注

    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]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 关于#html5#的问题:H5页面用户手机返回的时候跳转到指定页面例如(语言-javascript)
  • ¥15 无法使用此凭据登录,因为你的域不可用,如何解决?(标签-Windows)
  • ¥15 yolov9的训练时间
  • ¥15 二叉树遍历没有报错但无法正常运行
  • ¥15 在linux系统下vscode运行robocup3d上场球员报错
  • ¥15 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动