.htaccess - 使用 .htaccess 为多个参数重写 URL

标签 .htaccess

这个问题可能是重复的。但我没有找到任何适合我的解决方案。
我想重写 URL,其中我有一级和二级参数。第一个参数是 p第二个是 spwww.domain.com/home应该指向 www.domain.com/index.php?p=homewww.domain.com/projects/99应该指向 www.domain.com/index.php?p=projects&sp=99
我在 .htaccess 中怎么做?

目前我的 htaccess 如下,

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

最佳答案

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

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 - 使用 .htaccess 为多个参数重写 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14786340/

相关文章:

apache - htaccess : Redirect to https not working with out www

.htaccess ErrorDocument 的相对路径

apache - 处理 htaccess 中的尾部斜杠

linux - 将非 www 重定向到 www,但保持其他子域不变

html - .htaccess 重写单页 anchor 规则(seo 友好)

php - 从 header 下载时缺少文件扩展名

apache - 通过 .htaccess 将 http 重定向到 https 不起作用

php - 在使用 Symfony2 的 Apache 上,除了主页之外的每个页面上的 SSL 安装后 "Page Not Found"错误

http - 使用 .htaccess 将 http 重定向到 https 时,某些网址会出现奇怪的 401 错误

regex - 如何在 .htaccess 中使用正则表达式转义 "?"for mod_rewrite