简体   繁体   English

.htaccess中的RewriteRule循环不起作用

[英]RewriteRule loop in .htaccess not working

I'm trying to make pretty links for all the pages in a site, I'd like foo.com/services/domestic to go to foo.com/index.php?page=services-domestic . 我正在尝试为网站中的所有页面建立漂亮的链接,我想foo.com/services/domestic转到foo.com/index.php?page=services-domestic I have the following in my .htaccess file and it's working fine for the above example, but if I try to go to foo.com/services/domestic/case/1 (expecting /index.php?page=services-domestic-case-1 ) I get a 404. 我的.htaccess文件中包含以下内容,对于上面的示例来说,它工作正常,但是如果我尝试转到foo.com/services/domestic/case/1 (预期/index.php?page=services-domestic-case-1 )我得到404。

# a/b/c/d -> a-b-c-d
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9_-]+)$ $1-$2 [N]

# a-b-c-d -> index.php?page=a-b-c-d
RewriteRule ^([^(/|\.)]+)$ index.php?page=$1

I've tried just repeating the [N] line three times without the flag to no avail. 我试过只重复[N]行3次而没有标志无效。 It's not working for any but the first depth. 除了第一个深度,它没有任何作用。 Probably just my amateur regex but I can't understand what's breaking, any ideas? 可能只是我的业余正则表达式,但我不明白发生了什么变化,有什么想法吗?

Try this : 尝试这个 :

RewriteRule ^([\w-]+)/([\w-]+)$ $1-$2 [N]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?page=$1

It looks like you just need to tweak your regular expressions and rewrite flags: 看来您只需要调整正则表达式并重写标志:

# a/b/c/d -> a-b-c-d
RewriteRule ^(.*)/(.*) /$1-$2 [L]

# a-b-c-d -> index.php?page=a-b-c-d
RewriteRule ^([^/.]+)$ /index.php?page=$1 [L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM