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

报告相同问题?

悬赏问题

  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动