Apache mod_rewrite and Examples

Mod Reword allows you to change the URL that everyone recognizes when they check out your domain or a particular address, path. Just add the following rule given below to your .htaccess file located in public_html folder.

Please keep in mind we do not offer assistance to code this, nor do we guarantee to make your rule perform. Some of these requirements perform along with others, but some do not.

mod_rewrite 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

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 .htaccess Coding



  • 169 Los Usuarios han Encontrado Esto Útil
¿Fue útil la respuesta?

Related Articles

I Forgot My Password and Need a Reset

Below you will learn more on how to modify security passwords for various records relevant to...

How do I backup / restore / move from one cPanel host to another?

How can I recover my web page with old/previous content?Please contact us through Ticket at...

Accessing Web Disk and WebDav

Web Disk (WebDAV) is simply a drag-and-drop interface in cPanel which allows you to access your...

How to copy a database in phpMyAdmin

    Choose the database which you would like to duplicate (by simply clicking the...

How to Set Up a Redirect

    Log into cPanel.    Under Domains, simply select the Redirect...