在apache中搜索和替换htaccess一个RewriteRule

时间:2021-11-21 11:18:31

I'd basically like to get

我基本上想要得到

/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实现中正则表达式的限制,我还没有看到一种直接的方法。

4 个解决方案

#1


Try this rule:

试试这条规则:

RewriteRule ^([^+]*)\+(.*) $1\ $2 [N]

#2


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文件中的ex),因为第一行实际上是一个循环,它再次通过所有规则(因为N标志)

RewriteRule ^/redirect/from/([^_]*)\_(.*)$ /redirect/from/thisisthethingwedontneed$1-$2 [N,L]

RewriteCond %{REQUEST_URI} (thisisthethingwedontneed)+

RewriteCond%{REQUEST_URI}(thisisthethingwedontneed)+

RewriteRule (thisisthethingwedontneed)+(.*) /url/to/redirect/to/$2 [NC,QSA,R=301]

RewriteRule(thisisthethingwedontneed)+(。*)/ 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

第二部分:在捕获的第一部分之前插入一些可能在您的网址中找不到的文本,然后附加第一部分,然后是破折号,然后是第二部分

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:永久移动的重定向

#3


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,则可能会将该部分网址(PathInfo)视为查询字符串参数。

#4


I have a better one - with PHP :

我有一个更好的 - 用PHP:

.htaccess:

  RewriteCond %{REQUEST_URI} ^/word/[^/]+$
  RewriteRule ^word/(.*)$  http://example.com/special_redirect.php?q=$1 [L,QSA,R=301]

./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...

以上这一点可以应对德国的变音符号,但它适用于“_”到“ - ”这样简单的情况......等等......

#1


Try this rule:

试试这条规则:

RewriteRule ^([^+]*)\+(.*) $1\ $2 [N]

#2


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文件中的ex),因为第一行实际上是一个循环,它再次通过所有规则(因为N标志)

RewriteRule ^/redirect/from/([^_]*)\_(.*)$ /redirect/from/thisisthethingwedontneed$1-$2 [N,L]

RewriteCond %{REQUEST_URI} (thisisthethingwedontneed)+

RewriteCond%{REQUEST_URI}(thisisthethingwedontneed)+

RewriteRule (thisisthethingwedontneed)+(.*) /url/to/redirect/to/$2 [NC,QSA,R=301]

RewriteRule(thisisthethingwedontneed)+(。*)/ 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

第二部分:在捕获的第一部分之前插入一些可能在您的网址中找不到的文本,然后附加第一部分,然后是破折号,然后是第二部分

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:永久移动的重定向

#3


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,则可能会将该部分网址(PathInfo)视为查询字符串参数。

#4


I have a better one - with PHP :

我有一个更好的 - 用PHP:

.htaccess:

  RewriteCond %{REQUEST_URI} ^/word/[^/]+$
  RewriteRule ^word/(.*)$  http://example.com/special_redirect.php?q=$1 [L,QSA,R=301]

./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...

以上这一点可以应对德国的变音符号,但它适用于“_”到“ - ”这样简单的情况......等等......