在 apache htaccess 中搜索并替换一个 RewriteRule
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/919378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Search and replace in apache htaccess a RewriteRule
提问by Paul Irish
I'd basically like to get
我基本上想得到
/path/file+name+with+plusses.mp3
/path/file+name+with+plusses.mp3
to rewrite to
改写为
/path/file name with plusses.mp3
/path/file name with plusses.mp3
In my case wordpress is intercepting the request and giving a 404. But the file does indeed exist.
就我而言,wordpress 正在拦截请求并给出 404。但该文件确实存在。
Given the constraints of the regex in mod_rewrite implementation, I haven't yet seen a straightforward way of doing this.
鉴于 mod_rewrite 实现中正则表达式的限制,我还没有看到这样做的直接方法。
回答by Gumbo
Try this rule:
试试这个规则:
RewriteRule ^([^+]*)\+(.*) \ [N]
回答by Rau
Well, I do have a solution, but I don't really like it... This will replace all underscores with dashes, and redirects with a 301 status code, which is the 'Moved permanently'. (of course you can use it to replace any other chars too)
好吧,我确实有一个解决方案,但我不太喜欢它...这将用破折号替换所有下划线,并使用 301 状态代码重定向,即“永久移动”。(当然你也可以用它来替换任何其他字符)
Also, it should be the first rule (for ex in the .htaccess file) because the first line is a loop actually, which goes through all the rules again (because of the the N flag)
此外,它应该是第一条规则(例如在 .htaccess 文件中),因为第一行实际上是一个循环,它再次遍历所有规则(因为 N 标志)
RewriteRule ^/redirect/from/([^_]*)\_(.*)$ /redirect/from/thisisthethingwedontneed- [N,L]RewriteCond %{REQUEST_URI} (thisisthethingwedontneed)+
RewriteRule (thisisthethingwedontneed)+(.*) /url/to/redirect/to/$2 [NC,QSA,R=301]
RewriteRule ^/redirect/from/([^_]*)\_(.*)$ /redirect/from/thisisthethingwedontneed- [N,L]RewriteCond %{REQUEST_URI} (this isthething wedontneed)+
RewriteRule (this isthethingwdontneed)+(.*) /url/to/redirect/to/$2 [NC,QSA,R=301]
Explanation:
解释:
First line:
第一行:
'redirect/form' : the base path or anything you want to redirect from. It should be included in the second part, to be able to match it at the next run of the loop
'redirect/form' : 基本路径或任何你想重定向的路径。它应该包含在第二部分中,以便能够在循环的下一次运行中匹配它
first part: 'replace (not underscore) followed by an underscore followed by (anything)' an capture the first and last part for later use
第一部分:“替换(不是下划线)后跟下划线后跟(任何东西)”捕获第一部分和最后一部分供以后使用
second part: insert some text what is likely not found in your urls before the captured first part, then append the first part, then the dash, then the second part
第二部分:在捕获的第一部分之前插入一些可能在您的 url 中找不到的文本,然后附加第一部分,然后是破折号,然后是第二部分
flags: N : after this, go ahead again, execute all rewrite rules again, but with the altered url L : if there was a match, stop here (the 2 flags together actually make the thing what you would expect from the first one.)
flags: N :在此之后,再次继续,再次执行所有重写规则,但使用更改后的 url L :如果匹配,则停在此处(这 2 个标志一起实际上使您对第一个标志产生了期望。 )
Second line
第二行
Condition for the next rule: execute the next rule only if the previously defined string can be found in the request uri, at least one times
下一条规则的条件:只有在请求 uri 中可以找到先前定义的字符串时,才执行下一条规则,至少执行一次
Third line
第三行
First part: match and capture any occurrences of the funny string, and capture everything after it
第一部分:匹配并捕获所有出现的有趣字符串,并捕获它之后的所有内容
Second part: append the second part to any path we want to redirect to (and forget about the funny string)
第二部分:将第二部分附加到我们想要重定向到的任何路径(忘记有趣的字符串)
Flags: NC: case insensitive QSA: append any query string R=301: redirect with moved permanently
标志:NC:不区分大小写 QSA:附加任何查询字符串 R=301:重定向并永久移动
回答by Vladimir
I have a better one - with PHP :
我有一个更好的 - 使用 PHP :
.htaccess:
.htaccess:
RewriteCond %{REQUEST_URI} ^/word/[^/]+$
RewriteRule ^word/(.*)$ http://example.com/special_redirect.php?q= [L,QSA,R=301]
./special_redirect.php
./special_redirect.php
<?php
$q = $_GET['q'];
$new = strtolower(convert_character($q));
//echo "$q | $new";
$location = "http://" . $new . ".example.com/";
//echo $location;
function convert_character($name) {
$patterns_raw = array('?', '?', '?', '?', 'ü', 'ü', '?');
foreach ($patterns_raw as $pattern_raw) {
$patterns[] = '/' . $pattern_raw . '/u';
}
$replacements = array('ae', 'ae', 'oe', 'oe', 'ue', 'ue', 'ss');
$new_name = preg_replace($patterns, $replacements, $name);
return $new_name;
}
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: $location" );
?>
This above copes with German umlaut chars but it is adaptable for the simpler cases of "_" to "-" and so on...
以上处理了德语变音字符,但它适用于“_”到“-”等更简单的情况......
回答by Jordan S. Jones
I am not entirely convinced mod_rewrite will solve this problem for you as the +is a perfectly legal query string character. If you have Wordpress installed into the root of your website, it may be treating that portion of the url (PathInfo) as a query string parameter.
我并不完全相信 mod_rewrite 会为您解决这个问题,因为它+是一个完全合法的查询字符串字符。如果您将 Wordpress 安装到网站的根目录中,它可能会将 url (PathInfo) 的那部分视为查询字符串参数。

