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