问题更换下划线连字符用的.htaccess [英] Issues Replacing underscores to hyphens with .htaccess

查看:216
本文介绍了问题更换下划线连字符用的.htaccess的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我已经看到了这个塞马几个帖子,我仍然有一些问题,以达到我想要的。 我的旧网址的人,如:

Although I already saw few posts about this thema, I am still having some problems to achieve what I want. My old URL's were like:

http://myhostname.com/offer/1234_Nice_Offer_Cool_Whatever_HEllyeah.htm

和很明显,我想将它们改写为:

And obviously I want to rewrite them to:

http://myhostname.com/offer/1234-nice-offer-cool-whatever-hellyeah.htm

这是在code我尝试在.htaccess。这工作正常,以取代在URL的下划线,他们没有任何的目录:

This is the code I tried in .htaccess. This works fine to replace the underscores in URL's which they don't have ANY directory:

RewriteCond %{REQUEST_URI} \.htm$ [NC]    
RewriteRule ^([^_]*)_+(.*)$ $1-$2 [E=underscores:Yes,N] 
RewriteCond %{ENV:underscores} ^Yes$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

所以,这PAIRE规则正常工作与网址,如:

So, this paire of rules work fine with URL's like:

http://myhostname.com/1234_Some_Section_hellyeah.htm

但是当我尝试在同一$ C $下的URL,如:

But when I try the same code for URL's like:

http://myhostname.com/dir1/dir2/dir3/1234_Some_Offer_Or_Section.htm

然后,服务器使一个无限循环([N]标志的我的身影......) 基本上,我想知道以何种方式影响每个目录的URL规则,为什么我会收到这个无限循环。谢谢!

Then, the server makes an infinite loop (I figure of the [N] flag...) Basically, I would like to know in which way affects a per-directory URL to these rules and why am I getting this infinite loop. Thanks!

推荐答案

您不必N标记。要通过连字符代替下划线递归以下code将工作:

You don't need N flag. To replace underscore by hyphen recursively following code will work:

RewriteRule ^([^_]+)_(.+?\.htm)$ $1-$2 [L,NC,E=underscores:Yes]

RewriteCond %{ENV:REDIRECT_underscores} ^Yes$
RewriteRule ^([^_]+)$ /$1 [R=301,L]

这篇关于问题更换下划线连字符用的.htaccess的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆