我想重定向包含下划线同一个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的,我想,以取代下划线与连字符,然后将用户重定向的新网址
发布评论