用的.htaccess多个参数重写URL [英] Rewrite URL with .htaccess for multiple parameters

查看:337
本文介绍了用的.htaccess多个参数重写URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题可能是重复的。但我没有找到适合我的任何解决方案。 我想重写网址,在那里我有一个和两个级别的参数。第一个参数是 P 键,第二个是 SP

This question might be a duplicate. But I did not find any solution worked for me. I want to rewrite URL, where I have one and two level parameters. first parameter is p and second is sp

www.domain.com/home 应指向 www.domain.com/index.php?p=home www.domain.com/projects/99 应指向 www.domain.com/index.php?p=projects&sp=99

www.domain.com/home should point to www.domain.com/index.php?p=home and www.domain.com/projects/99 should point to www.domain.com/index.php?p=projects&sp=99

我该怎么做在.htaccess?

How do I do in .htaccess?

目前我的htaccess是为followes,

Currently My htaccess is as followes,

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?p=$1
RewriteRule ^([^/]*)/([^/]*)\$ index.php?p=$1&sp=$2 [L]

与此htaccess的问题是,它正确地指向一个级别网址。即,www.domain.com/home。但不是二级URL。 IE浏览器。 www.domain.com/projects/99

The problem with this htaccess is that it correctly points one level url. ie., www.domain.com/home. But not the two level url. ie. www.domain.com/projects/99

推荐答案

您必须分开处理的规则。所有的条件preceding规则只适用于一个单一的规则。以下规则不是该规则感动。您试图'链'两条规则。第二条规则永远也不可能一致,因为第一个是一个包罗万象的,改变的语法。除此之外,你必须确保在第一个规则不捕获不必要的请求。同时考虑是否要使用 * + 运营商的正则表达式。我建议你​​使用 + 操作符,让你有当被请求的页面或子页面空值一个明确的错误信息。

You have to treat the rules separately. All Conditions preceding rules only apply to a single rule. Following rules are not touched by that rule. You tried to 'chain' two rules. The second rule never could have matched, since the first one was a catch-all that changed the syntax. Apart from that you have to make sure that the first rule does not catch unwanted requests. Also think about whether you want to use the * or the + operator in the regexes. I suggest you use the + operator, so that you have a clear error message when empty values are requested for a 'page' or a 'subpage'.

所以这可能更接近你在找什么:

So this might come closer to what you are looking for:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?p=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?p=$1&sp=$2 [L]

这篇关于用的.htaccess多个参数重写URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆