.htaccess – how to force “www.” in a generic way?

I would use this rule: RewriteEngine On RewriteCond %{HTTP_HOST} !=”” RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteCond %{HTTPS}s ^on(s)| RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] The first condition checks whether the Host value is not empty (in case of HTTP/1.0); the second checks whether the the Host value does not begin with www.; the third checks for HTTPS (%{HTTPS} … Read more

How to deny access to a file in .htaccess

Within an htaccess file, the scope of the <Files> directive only applies to that directory (I guess to avoid confusion when rules/directives in the htaccess of subdirectories get applied superceding ones from the parent). So you can have: <Files “log.txt”> Order Allow,Deny Deny from all </Files> For Apache 2.4+, you’d use: <Files “log.txt”> Require all … Read more

Redirect non-www to www in .htaccess

Change your configuration to this (add a slash): RewriteCond %{HTTP_HOST} ^example.com$ [NC] RewriteRule (.*) http://www.example.com/$1 [R=301,L] Or the solution outlined below (proposed by @absiddiqueLive) will work for any domain: RewriteEngine On RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] If you need to support http and https and preserve the protocol choice try the following: … Read more

How does RewriteBase work in .htaccess

RewriteBase is only applied to the target of a relative rewrite rule. Using RewriteBase like this… RewriteBase /folder/ RewriteRule a\.html b.html is essentially the same as… RewriteRule a\.html /folder/b.html But when the .htaccess file is inside /folder/ then this also points to the same target: RewriteRule a\.html b.html Although the docs imply always using a … Read more

.htaccess redirect all pages to new domain

The below answer could potentially cause an infinite redirect loop… Here, this one redirects everything after the domain name on the URL to the exact same copy on the new domain URL: RewriteEngine on RewriteRule ^(.*)$ http://www.newdomain.com/ [R=301,L] www.example.net/somepage.html?var=foo redirects to: www.newdomain.com