开发者

htaccess back-reference returning no value for certain matches

开发者 https://www.devze.com 2023-03-06 09:36 出处:网络
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

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-ref开发者_JAVA百科erence 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!


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号