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

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

问题描述

这个问题可能是重复的.但我没有找到任何适合我的解决方案.我想重写 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=homewww.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如下,

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 的问题是它正确地指向一级 url.即,www.domain.com/home.但不是二级网址.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

推荐答案

你必须单独对待规则.规则之前的所有条件仅适用于单个规则.以下规则不受先行规则及其条件的影响.你试图链接"两个规则.第二个规则永远不可能匹配,因为第一个规则是一个改变了语法的包罗万象的规则.除此之外,您必须确保第一条规则不会捕获不需要的请求.还要考虑是否要在模式中使用 *+ 运算符.我建议您使用 + 运算符,以便在为页面"或子页面"请求空值时有明确的错误消息.

You have to treat the rules separately. All Conditions preceding rules only apply to a single rule. Following rules are not touched by peceding rule and their conditions. 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 patterns. 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天全站免登陆