我想将任何包含下划线的请求重定向到同一个网址,但用连字符代替网址.我看到很多论坛都建议我做这样的事情:
(重复这个 rewriteRule 的增量版本,最多一亿次)
rewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)$ example/$1-$2-$3-$4 [R=301,L]重写规则 ^([^_]*)_([^_]*)_(.*)$ example/$1-$2-$3 [R=301,L]重写规则 ^([^_]*)_(.*)$ example/$1-$2 [R=301,L]我目前在 php 中的解决方案是(工作正常):
if(preg_match('/[_]+/', $_SERVER['REQUEST_URI'])){$redirectUrl = preg_replace('/[_]+/','-', $_SERVER['REQUEST_URI']);标头('位置:'.$redirectUrl , TRUE, 301);返回;}但是,我宁愿不使用 php &而是将它保存在我的 htaccess 文件中,而不必一遍又一遍地重复第一个 rewriteRule 以预测每个 url 可能有多少个下划线.想法?有没有这样一种方法可以在不重复递增的 rewriteRule 的情况下做到这一点?
----- 编辑 -----
我当前的 htaccess 文件只是标准的 WordPress 文件,如下所示:
重写引擎开启重写基数/重写规则 ^index.php$ - [L]RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d重写规则./index.php [L]</IfModule> 解决方案查看Next"指令/重写标志,这会导致从第一条规则开始重新评估规则集:httpd.apache/docs/current/rewrite/flags.html#flag_n>
这样的事情可能会奏效:
RewriteRule (.*)_(.*) $1-$2 [N]I'd like to redirect any request that contains underscores to the same url, but with hyphens replacing the urls. I've seen a lot of forums suggest I do something like this:
(Repeat an incremented version of this rewriteRule up to a gazillion)
rewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)$ example/$1-$2-$3-$4 [R=301,L] rewriteRule ^([^_]*)_([^_]*)_(.*)$ example/$1-$2-$3 [R=301,L] rewriteRule ^([^_]*)_(.*)$ example/$1-$2 [R=301,L]My current solution in php is (which works fine):
if(preg_match('/[_]+/', $_SERVER['REQUEST_URI'])){ $redirectUrl = preg_replace('/[_]+/','-', $_SERVER['REQUEST_URI']); header('Location: '. $redirectUrl , TRUE, 301); return; }But, I'd rather not use php & instead keep it in my htaccess file without having to repeat that first rewriteRule over and over again in order to anticipate how many underscores each url might be. Thoughts? Is there such a way to do this with out repeating the incremented rewriteRule?
----- edit -----
My current htaccess file is just the standard WordPress one, which is below:
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule>解决方案
See the "Next" directive/rewrite flag, which causes the ruleset to be reevaluated starting with the first rule: httpd.apache/docs/current/rewrite/flags.html#flag_n
Something like this might work:
RewriteRule (.*)_(.*) $1-$2 [N]
更多推荐
在 htaccess 中,我想用连字符替换下划线,然后将用户重定向到新的 url
发布评论