In .htaccess, how to redirect non-lowercase hostname versions

In .htaccess on Apache2, how you redirect all host capitalization variations to the canonical lowercase version through 301 redirects and keep the rest of the path safe and sound. Subdomains (or not) should do the same.

In addition, access via IP should not be redirected.

examples:

  • http://Example.com/foo => http://Example.com/foo
  • http://A.example.com/foo => http://A.example.com/foo
  • http://A.EXample.com/foo?bar => http://A.EXample.com/foo?bar
  • http://208.67.222.222/foo => http://208.67.222.222/foo
+1
source share
2 answers
 # Make sure hostname is lowercase only (or an IP address) RewriteCond %{HTTP_HOST} !^(.+\.)?example\.com$ RewriteCond %{HTTP_HOST} !^[\d\.]{7,15}$ RewriteRule ^(.*)$ ${lowercase:%{HTTP_HOST}}/$1 [R=301,L] 
+1
source

I searched and could not find a solution on the Internet that covers any number of domains. The use case for me is that I work on localhost, and so the first line (with example.com) will not work on both localhost and my domain, as well as any other name that someone uses instead localhost.

To add an answer to @philfreo, therefore: (copy lines, but change only the first)

 # Make sure hostname is lowercase only (or an IP address) RewriteCond %{HTTP_HOST} !^(.+\.)?(.+)?$ RewriteCond %{HTTP_HOST} !^[\d\.]{7,15}$ RewriteRule ^(.*)$ ${lowercase:%{HTTP_HOST}}/$1 [R=301,L] 

Reperts to Filfreo! A lot of time was saved with his answer.

Paragon

0
source

Source: https://habr.com/ru/post/970242/


All Articles