views:

1600

answers:

5

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.

+3  A: 

Try this rule:

RewriteRule ^([^+]*)\+(.*) $1\ $2 [N]
Gumbo
In my case this didn't seem to do anything, but even if it did... It looks to replace a single + with space, but only the first. Am I wrong about that?Also it's worth nothing that changing the + to %20 would be just peachy, too. :)
Paul Irish
The N flag will cause a repetition (same as `continue` in programming languages) until there is no plus character left. And for me it works like a charm.
Gumbo
A: 

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.

Jordan S. Jones
A: 

can you explain me how this does work?

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

i need to rewrite all "-" to "_"

in my last part of the adress

greeen
+1  A: 

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

Rau
A: 

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

Vladimir