在 htaccess 中,我想用连字符替换下划线,然后将用户重定向到新的 url

编程入门 行业动态 更新时间:2024-10-28 06:26:30
本文介绍了在 htaccess 中,我想用连字符替换下划线,然后将用户重定向到新的 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想将任何包含下划线的请求重定向到同一个网址,但用连字符代替网址.我看到很多论坛都建议我做这样的事情:

(重复这个 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

本文发布于:2023-10-31 18:23:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546902.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:下划线   想用   到新   重定向   字符

发布评论

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

>www.elefans.com

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