Search and replace in apache htaccess a RewriteRule

余生颓废 提交于 2019-12-28 04:30:08

问题


I'd basically like to get

/path/file+name+with+plusses.mp3

to rewrite to

/path/file name with plusses.mp3

In my case wordpress is intercepting the request and giving a 404. But the file does indeed exist.

Given the constraints of the regex in mod_rewrite implementation, I haven't yet seen a straightforward way of doing this.


回答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)

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)

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

RewriteCond %{REQUEST_URI} (thisisthethingwedontneed)+

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

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

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

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




回答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.




回答4:


I have a better one - with 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...



来源:https://stackoverflow.com/questions/919378/search-and-replace-in-apache-htaccess-a-rewriterule

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!