1

I'm looking into setting up a re-write to ignore the case of the directory name of the URL.

For example, I have

example.com/TestDirectory

I want to ignore the case so the URL will still direct to

example.com/testdirectory

Here is what I have, but it doesn't seem to be working:

# rewrite rules for example.com/TestDirectory
RewriteCond %{http_host} ^example\.com [NC,OR]
RewriteCond %{http_host} ^www\.example\.com\TestDirectory [NC]
RewriteRule $ http://www.example.ccom/testdirectory/

Am I missing a step?

MrWhite
  • 11,643
  • 4
  • 25
  • 40
POPEYE1716
  • 39
  • 1
  • 5
  • http_host does not contain directories. It's a hostname, not a full URL. –  Jun 07 '17 at 22:14

1 Answers1

1

In order to implement a canonical redirect to the correctly cased directory (in this case, all lowercase) you could do something like the following:

RewriteCond %{REQUEST_URI} !^/testdirectory
RewriteRule ^testdirectory /testdirectory/ [NC,R,L]

The NC (nocase) flag on the RewriteRule ensures that it would match TestDirectory or TESTDIRECTORY etc. and the RewriteCond directive prevents a redirect loop by checking that we are not already at testdirectory via a case-sensitive match.

However, this only redirects to the directory. Any file after the directory name is lost. In order to redirect /TestDirectory/<whatever> to /testdirectory/<whatever> then try something like the following instead:

RewriteCond %{REQUEST_URI} !^/testdirectory
RewriteRule ^testdirectory/?(.*) /testdirectory/$1 [NC,R,L]

Note that this is a temporary (302) redirect. Change the R flag to R=301 if this is intended to be permanent, but only after you have checked that it is working OK.

RewriteCond %{http_host} ^www\.example\.com\TestDirectory [NC]

Note that the HTTP_HOST server variable contains just the requested hostname. There is no URL-path information. If you need to check the host as well (if you have multiple domains) then include an additional condition:

RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]

(This assumes the domain has already been canonicalised to include the www subdomain.)

MrWhite
  • 11,643
  • 4
  • 25
  • 40