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.

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

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

Guides to dot-htaccess coding:
javascriptkit.com
apache.org
modrewrite.com
Great resource for mod rewrite lessons.
Great resource for mod rewrite examples.

Related Articles

  • .htaccess guidance

 

.htaccess guidance

.htaccess is a special Apache file that tells your website how to function.

You can edit the existing .htaccess file on your site, or you can create a new text file in any directory and name it .htaccess

If you do not code this correctly, it can cause website errors.

Guides to dot-htaccess coding:
javascriptkit.com
apache.org
modrewrite.com
Great resource for mod rewrite lessons.
Great resource for mod rewrite examples.

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

READ THIS BEFORE SETTING UP A SUBDOMAIN!

To create a subdomain, cPanel asks for the name you wish to use. MAKE SURE the subdomain name you...

READ THIS BEFORE SETTING UP AN ADDON DOMAIN!

Before you can add an addon domain you must change the name servers for the domain name to the...

What is the difference between shared SSL and private SSL?

A shared SSL displays as https://ip/~yourusername/. A private SSL would display as...

Backup policy, Do you provide backups?

You are responsible for your backups and web content. We create our own weekly backups on the...

CGI center

The following CGI Scripts are pre-installed and ready to use on your account:Simple CGI Wrapper...