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

发布于 2024-12-08 01:53:43 字数 1042 浏览 0 评论 0原文

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

重复此 rewriteRule 的增量版本,直至无数)

rewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)$ http://example.com/$1-$2-$3-$4 [R=301,L]
rewriteRule ^([^_]*)_([^_]*)_(.*)$ http://example.com/$1-$2-$3 [R=301,L]
rewriteRule ^([^_]*)_(.*)$ http://example.com/$1-$2 [R=301,L]

我当前在 php 中的解决方案是(效果很好):

if(preg_match('/[_]+/', $_SERVER['REQUEST_URI'])){
    $redirectUrl = preg_replace('/[_]+/','-', $_SERVER['REQUEST_URI']);
    header('Location: '. $redirectUrl , TRUE, 301);
    return;
}

但是,我宁愿不使用 php & ;相反,将其保留在我的 htaccess 文件中,而不必一遍又一遍地重复第一个 rewriteRule,以便预测每个 url 可能有多少个下划线。想法?有没有一种方法可以在不重复递增的 rewriteRule 的情况下做到这一点?

----- 编辑 -----

我当前的 htaccess 文件只是标准的 WordPress 文件,如下:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

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 ^([^_]*)_([^_]*)_([^_]*)_(.*)$ http://example.com/$1-$2-$3-$4 [R=301,L]
rewriteRule ^([^_]*)_([^_]*)_(.*)$ http://example.com/$1-$2-$3 [R=301,L]
rewriteRule ^([^_]*)_(.*)$ http://example.com/$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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苯莒 2024-12-15 01:53:43

请参阅“Next”指令/重写标志,这会导致从第一条规则开始重新评估规则集:http://httpd.apache.org/docs/current/rewrite/flags.html#flag_n

像这样的东西可能会起作用:

RewriteRule (.*)_(.*) $1-$2 [N] 

See the "Next" directive/rewrite flag, which causes the ruleset to be reevaluated starting with the first rule: http://httpd.apache.org/docs/current/rewrite/flags.html#flag_n

Something like this might work:

RewriteRule (.*)_(.*) $1-$2 [N] 
喜爱皱眉﹌ 2024-12-15 01:53:43

如何将 2 结合起来。使用单个重写规则来重写执行替换和重定向的 PHP 脚本。这样您就可以避免多个重写规则,可以处理任意数量的下划线,并且仅在需要时调用 PHP 重定向。

How about combining the 2. Have a single rewrite rule that rewrites to a PHP script that does the substitution and redirect. That way you avoid having multiple rewrite rules, can handle any number of underscores and only invoke the PHP redirection when it is needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文