views:

246

answers:

3

Hi, I am dealing with a situation where someone has handed me a bunch of old files on the server which already have a lot of incoming links directly to them (mostly pdf files). I now have the files organized but in a different directory. Before it was 'domain/manuals/file' now it is 'domain/media/manual/file'. I am trying to resolve this issue using an htaccess file.

Many of the files have spaces in the names (not something I can control) and because they already have links to them I can't just go through renaming them. I have found that I can redirect files individually when they have spaces in the names by using quotes such as:

redirect 301 "/manuals/file 123.pdf" "http://www.domain.com/manuals/file 123.pdf"

However, there are loads of these files and I wondered if there is a way to create a regular expression that will handle spaces in file names that I could use to redirect the entire directory. I should add that some files contain decent file names with no spaces in, some have one space and others more than one space. It's not pretty.

If you've encountered this problem before I would really appreciate hearing your advice, I'm running out of ideas.

Thanks.

A: 

What about:

RedirectMatch permanent ^/manuals/(.*)$ http://www.domain.com/media/manual/$1

The (.*) matches any character following /manuals/ in the URL.

Note: Redirect and RedirectMatch are part of mod_alias, not mod_rewrite (as the question was tagged) so forgive the earlier mod_rewrite rule I posted.

Andy Shellam
Thanks for the help guys.@Andy that worked nicely thanks. Am I right in thinking that if I use a standard 301 redirect for individual files after the RedirectMatch statement then I can also redirect urls for files where I have a different file name?Thanks.
James
I would say to redirect the filename changes first and go from `/manuals/old file.pdf` to `/media/manuals/new file.pdf`, otherwise the client will end up doing 2 redirects (one to change directory and one to change filename.)
Andy Shellam
I put my redirects for individual files before the RedirectMatch statement for the directory, is that what you meant?Thanks for you help.
James
So like this: redirect 301 "/manuals/old file.php" http://www.domain.com/manuals/new-file.php RedirectMatch permanent ^/manuals/(.*)$ http://www.domain.com/media/manuals/$1
James
Yes, but on your first example, point the redirect straight to the new directory - `redirect 301 /manuals/old-file.php domain.com/media/manuals/new-file.php` as that will save the client another redirect.
Andy Shellam
A: 

If you need characters for spaces in a URL, you can use %20 which is the URL Encoding of a space.

Justin Niessner
A: 

Redirect can work on whole directories at once.

Redirect permanent /manuals/ http://www.domain.com/manuals/

Then you don't have to worry about escaping spaces to %20, or any other character. Apache will redirect every file under that path to the same file under the destination URL.

(RedirectMatch exists as well for regex matching; you don't need to resort to that for this though.)

bobince
Thanks bobince that worked to
James