htaccess Files Best Practice Guide With Examples and Functions
RewriteEngine On #----------------------------------------- # Redirect Entire two Site to New Domain #----------------------------------------- # 1. RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR] RewriteCond %{HTTP_HOST} ^another.olddomain.com$ [NC] RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L] # 2. LoadModule rewrite_module modules/mod_rewrite.so <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_HOST} ^localhost$ [NC] RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L] </IfModule> # Redirect Entire Site to New Domain #----------------------------------------- RewriteCond %{HTTP_HOST} ^old.domain.com$ RewriteRule ^/?$ "http\:\/\/new\.domain\.com\/" [R=301,L # Redirect Entire Site to New Site #----------------------------------------- Redirect 301 / http://www.new-domain.com Redirect 301 /old-page-name http://www.your-domain.com/new-page-name # 301 Redirect Based on IP Address: #----------------------------------------- RewriteCond %{REMOTE_ADDR} ^(A\.B\.C\.D)$ RewriteRule ^/* http://www.yourdomain.com/access-denied.html [L] # Non-www to www Redirect # This is a very common function for redirecting your site # at the top level. Sometimes a site can be accessed at # http://domain.com/ and http://www.domain.com/ which can # cause duplication and site management issues. #----------------------------------------- RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] # Clean up URLs to remove file extensions # If you wanted to make your URLs look a little nicer by # removing the file extension from the end of it you can # use the function below. In order for this to work # internally to the site you should also ensure that all # the linking within the site has the extension manually # removed. #----------------------------------------- RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.html -f RewriteRule ^(.*)$ $1.html # Restrict access # To add an element of security to the .htaccess file you # should add the following function. It will deny access # to anyone attempting to get to the file externally to # view or change it. For example, if you were to add this # to your file and then attempt to access it at # http://www.domain.com/.htaccess you will be presented # with a 403 forbidden error. This means its working! #----------------------------------------- <Files .htaccess> order allow,deny deny from all </Files>
How to redirect HTTP traffic to HTTPS using an .htaccess file
The below code when added to an .htaccess file will automatically redirect any traffic destined for http: to https:
RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Have sub directory not be password protected using Apache’s .htaccess
here is a way just using .htaccess. I saw this answer elsewhere
Just create an .htaccess file in the non-password protected subdirectory with the content:
Satisfy any
Create a new password file
# htpasswd -c /home/www/.htpasswd jerry
Change or update password
# htpasswd /home/www/.htpasswd -users tom
# vi /home/www/html/.htaccess
AuthType Basic
AuthName „Restricted Access“
AuthUserFile /home/www/.htpasswd
Require user vivek
# tail -f /var/log/httpd/access_log
# tail -f /var/log/httpd/error_log
#########################################################
.htaccess Redirect Subdomain to External URL
#########################################################
If you are redirecting a subdomain to another domain then there shouldn’t be anything else in the .htaccess file other than this.
# This will redirect a subdomain to another domain
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yoursubdomain\.yourdomain\.com$ [NC]
RewriteRule ^(.*) http://www.newdomain.com/$1 [L,R]