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 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解