HostMonster Web Hosting Help

Mod_Rewrite

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. Some of these codes work in combination, and some do not.

EXAMPLES

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

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

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

#Prevent subfolder loading. This goes
# in htaccess for the primary domain
RewriteCond %{HTTP_HOST} ^primary.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.primary.com$
RewriteRule ^addon.com/?(.*)$ "http://www.addon.com/$1" [R=301,L]

#Prevent subdomain name loading.
#This goes in htaccess for the primary domain
RewriteCond %{HTTP_HOST} ^subname.primary.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.subname.primary.com$
RewriteRule ^(.*)$ "http://www.addon.com/$1" [R=301,L]

# 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
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ /folder/ [R=301,L]
</IfModule>

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

Knowledgebase Article 88,696 views bookmark tags: apache mod rewrite (updated 443 days ago)


Was this resource helpful?

Did this resolve your issue?


Please add any other comments or suggestions about this content:





Recommended Help Content

Preventing bandwidth theft using the mod rewrite engine and .htaccess (updated 637 days ago)
Knowledgebase Article 68,218 views tags: htaccess rewrite

Virtual directory support for PHP. (updated 1618 days ago)
Knowledgebase Article 55,857 views tags: directory php support

What are Apache Handlers? How do I setup customized Apache Handlers? (updated 1618 days ago)
Knowledgebase Article 60,042 views tags: apache

Related Help Content

How do I perform a URL redirect/rewrite using the .htaccess file? (updated 604 days ago)
Knowledgebase Article 490,254 views tags: htaccess redirect rewrite url

Hard refresh after changing the .htaccess (updated 430 days ago)
Knowledgebase Article 17,646 views tags: apache

What types of technical support do you offer me? (updated 1635 days ago)
Knowledgebase Article 51,301 views tags: email support

Does HostMonster supoprt FFMPEG support. (updated 357 days ago)
Knowledgebase Article 62,426 views tags: encoding ffmpeg flash flv flvtools module path perl support video

The redirection of www.yourdomain.com/default.html or index.html to http://www.yourdomain.com/index.php?act=whatever redirects it to http://www.yourdomain.com/index.php%3fact=whatever In other words, (updated 76 days ago)
Knowledgebase Article 616,898 views tags: htaccess messing mod php redirect redirects rewrite variable variables

Changing PHP versions, load custom Apache mods, and other Apache/PHP customizations (updated 27 days ago)
Knowledgebase Article 613 views tags: easyapache php vps whm

How do I use .html files to run SSI (Server Side Includes)? (updated 1359 days ago)
Knowledgebase Article 79,640 views tags: apache file script

What is the Scope of Technical Support for FrontPage?. (updated 976 days ago)
Knowledgebase Article 47,773 views tags: frontpage