Apache mod_rewrite and examples

What is mod_rewrite? Mod Rewrite allows you to change the URL that everyone sees when they visit your domain or a specific address. Just add the code to your .htaccess file (typically the one inside public_html).

Please remember we do not offer support to code this, nor do we promise to make your code work.


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

#Allow only specified IPs to access your site
deny from all
allow from
allow from

# 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]

# Set a default home directory, (this subfolder always loads)
# 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 multiple 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]


Guides to dot-htaccess coding:



Great resource for mod rewrite lessons.

Great resource for mod rewrite examples.

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

Delete a CPanel email address

To delete an email address: Click the Delete link corresponding to the account you wish to...

Accessing Cpanel

To access cPanel: Type: https://IP:2083 into your preferred Internet browser....

Add email address

To add a new email address: Type the email address to be created in the Email field....

Change your Cpanel email password

A secure password is one that contains no dictionary words and includes upper- and lower-case...

Change your CPanel email quota limit

The quota limit for an address defines the amount of mail (in Megabytes) that can be stored...