htaccess back

编程入门 行业动态 更新时间:2024-10-26 00:23:16
htaccess back-reference不返回某些匹配的值(htaccess back-reference returning no value for certain matches)

我的.htaccess中有重定向的问题非常奇怪。

我设定的规则是:

RewriteRule ^([a-zA-Z0-9\-]*)/$ ?mode=book&permalink=$1

它匹配多个单词短语: http://www.fernwoodpublishing.ca/About-Canada-Youth-and-Children/

和单词短语: http://www.fernwoodpublishing.ca/Kaandossiwin/

但是,如果它与那些单字短语匹配,没有任何连字符,则反向引用不会正确传递。 那么,上面的例子应该去哪里

?mode=book&permalink=Kaandossiwin

这将正常工作,而不是去

?mode=book&permalink=

这只是创造了一大堆错误并打破了一些东西。

我尝试了一种方法来解决这个问题,似乎没有任何帮助。 任何对htaccess更有经验的人都知道为什么在这些特定情况下不返回反向引用? (我已经用其他示例测试了它并且经历了相同的问题,因此我确信它在所有情况下都匹配,但只在匹配包含连字符的情况下返回后引用。

非常感谢!

I'm having a really strange issue with a redirect in my .htaccess.

The rule I have set up is:

RewriteRule ^([a-zA-Z0-9\-]*)/$ ?mode=book&permalink=$1

It matches both multi-word phrases: http://www.fernwoodpublishing.ca/About-Canada-Youth-and-Children/

and single-word phrases: http://www.fernwoodpublishing.ca/Kaandossiwin/

However, when it does match those single-word phrases, without any hyphens, the backreference isn't passed through properly. So, where the above example SHOULD go to

?mode=book&permalink=Kaandossiwin

which would work correctly, it instead goes to

?mode=book&permalink=

which just creates a whole slew of errors and breaks stuff.

I've tried a variant of ways to fix this, and nothing seems to be helping. Anyone more experienced with htaccess have any idea why the back-reference isn't being returned in these specific cases? (I've tested it with other examples and have experienced the same problem precisely, so I'm certain that it's matching in all cases, but only returning the back-reference in those cases where the match contains hyphens.

Thanks so much!

最满意答案

请尝试以下方法。 我认为分组是畸形的。

RewriteRule ^([\w-]+)/$ ?mode=book&permalink=$1

我还建议为你的链接添加前缀,因为这种URL格式非常宽松。 也就是说它匹配顶级字母数字,下划线或短划线的任何URL。 像下面这样的东西更灵活。

RewriteRule ^book/([\w-]+)/?$ book.php?permalink=$1

Try the following. I believe the grouping is malformed.

RewriteRule ^([\w-]+)/$ ?mode=book&permalink=$1

I'd also suggest prefixing your link as this URL format is pretty loose. That is to say it matches any URL with alphanumeric, underscore, or dash at the top level. Something like the following is more flexible.

RewriteRule ^book/([\w-]+)/?$ book.php?permalink=$1

更多推荐

本文发布于:2023-08-05 04:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1427277.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:htaccess

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!