htaccess code examples

Your .htaccess file is commonly used to change the way that people access or view your site. This can be particularly important if you change the structure of your site and need to "redirect" visitors to where the content is now stored.

Please note that we do not support custom coding and these examples are given for your reference only. A "Google search" will return many more examples.

# Never use www in the domain. Replace 'example.com' with your domain name
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?example\.com)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]

# Always use www in the domain. Replace 'example.com' with your domain name
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z.]+)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.%1example.com%{REQUEST_URI} [R=301,L]

#Specify a default home page (index page)
DirectoryIndex home.html

#Block specified IPs from accessing your site
Require all granted
Require not ip 125.0.0.1
Require not ip 125.0.0.2
Require not ip 125.0.0.3

#Allow only specified IPs to access your site
Require ip 125.0.0.4
Require ip 125.0.0.5

# Redirect all pages from olddomain.com to newdomain.com
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^olddomain.com$
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]

# Set a default home directory, (instead of your website root). Replace 'folder' with your subfolder name
RewriteEngine On
RewriteRule ^$ /folder/ [R=301,L]

# Rename a directory and force visitors to the new name. Replace 'old' with your old folder name. Replace 'new' with your new folder name
RewriteEngine on
RewriteRule ^/?old([a-z/.]*)$ /new$1 [R=301,L]

# Always use https for secure connections. Replace 'www.example.com' with your domain name (as it appears on your SSL certificate)
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

# Block traffic from unwanted referrers
RewriteEngine on
Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} badsite\.com [NC,OR]
RewriteCond %{HTTP_REFERER} badforum\.com [NC,OR]
RewriteCond %{HTTP_REFERER} badsearchengine\.com [NC]
RewriteRule .* - [F]

#Do not allow these file types to be called
RewriteEngine on
RewriteRule .*\.(jpg|jpeg|gif|png|bmp|exe|swf)$ - [F,NC]

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

How to run php code in an html page?

If you want to run php code in your html web pages we recommend that you rename them using the...

What does SSI (Server Side Includes) mean?

SSI or Server Side Includes are used to add contents of other files or variables from the server...

What is a shtml file type?

A web file type with the suffix of ".shtml" (rather than the usual ".html" or ".htm") indicates...

Internal Server Error - http 500 error

Internal server errors can be caused by a few different things. Please check the Error Logs in...

Bandwidth Limit Exceeded - http 509 error message

If you are seeing the following http 509 Bandwidth Limit Exceeded error it is because you have...

Powered by WHMCompleteSolution