在htaccess的,我想,以取代下划线与连字符,然后将用户重定向的新网址

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

我想重定向包含下划线同一个URL的请求,但用连字符替换的URL。我见过很多论坛的建议我做这样的事情:

(重复此重写规则了一个递增的版本,以一个极大)

重写规则^([^ _ *)_([^ _ *)_([^ _ *)_(。*)$ example / $ 1- $ 2- $ 3- $ 4 R = 301,L] 重写规则^([^ _ *)_([^ _ *)_(。*)$ example/$1-$2-$3 [R = 301,L] 重写规则^([^ _ *)_(。*)$ example/$1-$2 [R = 301,L]

在PHP我目前的解决方案是(工作正常):

如果(preg_match('/ [_] + /,$ _ SERVER ['REQUEST_URI'])){     $的redirectUrl = preg_replace('/ [_] + /, - ,$ _ SERVER ['REQUEST_URI']);     头('位置:'$的redirectUrl,TRUE,301);     返回; }

不过,我宁可不使用PHP和放大器;而不是把它放在我的htaccess的文件,而无需以预测有多少突出每个URL可能是一遍又一遍地重复一次重写规则。思考?有没有这样一种方式来与走出这样做重复的增加重写规则?

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

我目前htaccess文件仅仅是个标准的Word preSS之一,这是如下:

< IfModule mod_rewrite.c> RewriteEngine叙述上 的RewriteBase / 重写规则^指数\ .PHP $ - [L] 的RewriteCond%{} REQUEST_FILENAME!-f 的RewriteCond%{} REQUEST_FILENAME!-d 重写规则。的index.php [L] < / IfModule>

解决方案

请参阅下一步指令/改写标志,这将导致规则集从第一个规则重新评估:的 httpd.apache/docs/current/rewrite/flags.html#flag_n

这样的事情可能工作:

重写规则(。*)_(。*)$ 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的,我想,以取代下划线与连字符,然后将用户重定向的新网址

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

发布评论

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

>www.elefans.com

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