简体   繁体   中英

Case insensitive rewrite rule

So I have a rewrite rule that it's needed because of the old site, and have some images that are linked from another website, the problem is that I can' manually fix the url's because there are a lot of images.

So before the website was hosted on Windows, and there was no problem if you want to link an image like this:

http://www.example.com/Fder69.JPG and the filename was "fder69.JPG" it did work, now I have a rewrite rule like this:

RewriteRule ^([^/.]+.JPG)$ /imgs/$1 [L,NC,R=302] so basicly rewrites the old links to the new structure, but some of the images that don't have the exact filename don't work.

Is there a way to accomplish this? with something like CheckSpelling Off or ? can I make the rewrite cond to accept .JPG and .jpg, any tips?

One option is to rename all the files to be all-lowercase, which generally leads to nicer URLs, and then redirect any requests for mixed-case versions to all lowercase.

This approach has the advantage that each file ends up with only a single URL, rather than the same content appearing under multiple URLs as would be the case if you used mod_speling . This is good for search engine rankings, among other things.

One way to rename all the files would be to generate a bunch of mv commands in a shell script, like this:

find . | perl -ne 'chomp; print "mv \"", $_, "\" \"", lc $_, "\"\n";' > rename-files.sh

Note that I make no warranties that this won't mess up all your files, but I think it's right...

The redirection is done using a "RewriteMap", which is a function which can be applied on the right hand side of a RewriteRule. One of the built-in mappings available is int:tolower , allowing you to do this:

# Alias the mapping function as "lc"
RewriteMap  lc int:tolower
# Perform the substitution if the URL contains uppercase letters
RewriteCond %{REQUEST_URI} [A-Z]
# Issue a 301 redirect to the all-lowercase version
RewriteRule /(.*) /${lc:$1} [R=permanent,L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM