Rgd htaccess重写多个查询

时间:2014-07-20 17:23:56

标签: php apache .htaccess mod-rewrite

下面是我的.htaccess文件

RewriteEngine On

RewriteRule ^(.*)/(.*)$ /manage/member/index.php?action=$1&id=$2 [L]
RewriteRule ^(.*)/(.*)/(.*)$ /manage/member/index.php?action=$1&id=$2&sub_action=$3 [L]
RewriteRule ^(.*)/(.*)/(.*)/(.*)$ /manage/member/index.php?action=$1&id=$2&sub_action=$3&sub_id=$4 [L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

DirectoryIndex index.php

我在前几行获得了这个$ _GET

$action = $_GET['action'];
$id = $_GET['id'];
$sub_action = $_GET['sub_action'];

如果我发送了一个像

这样的网址
http://subdomain.mydomain.com/manage/member/edit/5

它可以将操作作为编辑和身份识别为5

但如果我发送

http://subdomain.mydomain.com/manage/member/edit/5/permission

无法将sub_action检索为权限。我应该怎么做才能让它运作

它检索5为$ 1,权限为$ 2,$ 3为null。

我应该使用什么.htaccess来允许$ 3和$ 4为null或值,但是当它们有价值时可以通过$ _GET检索。

1 个答案:

答案 0 :(得分:1)

/manage/.htaccess

中保留这样的规则
DirectoryIndex index.php
RewriteEngine On
RewriteBase /manage/

# skip rewrite rules for all files and directories    
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^([^/]+)/([^/]+)/?$ member/index.php?action=$1&id=$2 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ member/index.php?action=$1&id=$2&sub_action=$3 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ member/index.php?action=$1&id=$2&sub_action=$3&sub_id=$4 [L,QSA]


# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]