mod_rewrite一个双目录

时间:2012-03-02 23:40:35

标签: regex .htaccess mod-rewrite url-rewriting get

我在mod_rewrite中使用.htaccess将双目录结构更改为双GET查询字符串,如下所示:

网址:http://domain.com/test/me/

在mod_rewrite之后:http://domain.com/index.php?u=test&c=me

在我的.htaccess文件中使用以下代码:

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/$ /index.php?u=$1&c=$2 [L]

效果很好,但是如果没有指定第二个目录(例如http://domain.com/test/),我希望c变量等于“all”,如下所示:

http://domain.com/index.php?u=test&c=all

我该怎么做? 谢谢,正则表达式看起来像克林贡诗歌。我尝试了上述代码的一些不同变体但没有成功。

P.S。如果您可以添加尾随/,即使未在网址框中输入任何内容http://domain.com/test/me,也可以使用http://domain.com/test/me/处理与http://domain.com/test相同且http://domain.com/test/被视为奖励积分与{{1}}

相同

1 个答案:

答案 0 :(得分:2)

喜欢这个吗?

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)\/?$ index.php?u=$1&c=all
RewriteRule ^([^/]*)/([^/]+)\/?$ index.php?u=$1&c=$2 [L]

还有奖金:P

相关问题