Apache mod_rewrite examples

What is mod_rewrite?

What is mod_rewrite?

Net Solution Hosting uses Apache, an open source HTTP server program, to host your sites. Apache can be customized via modules, one of the mod_rewrite modules that can: redirect a URL to another URL, rewrite required URLs, limit access to your site, and more.

A rewrite rule can be invoked by placing these rules in the .htaccess file. Below are examples where you can use these rules to customize the module according to your needs.

Notă: Please note that we do not support this code, nor do we promise to make this code for you. Some of these codes work in combination but others do not.


Exemple mod_rewrite

Here are some examples of rules that let you do the following:

Set Default Homepage

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

Specify IP Access

#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 Website Pages

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

Redirect individual files

# To redirect individual files, like example.com/oldfile.htm to newfile.htm you can use a 301 redirect like this
Redirect 301 /oldfile.htm /newfile.htm

Prevent Sub-folder Access

#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 Sub-domain Access

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

Force Non-www Domain Access

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

Force www Domain Access

# 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 Default Home Folder

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

Redirect Folder Access

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

Force HTTPS Access

# 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 Multiple Referrers Traffic

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

Deny File Type Access

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

Remove Index

Options +FollowSymLinks -MultiViews -indexes
RewriteEngine On
RewriteBase /
# remove index
RewriteCond %{THE_REQUEST} /index(\.php)?[\s?/] [NC]
RewriteRule ^(.*?)index(/|$) /$1 [L,R=301,NC,NE]

Remove .php

Options +FollowSymLinks -MultiViews -indexes
RewriteEngine On
RewriteBase /
# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [L,R=301]

Remove Index and /

Options +FollowSymLinks -MultiViews -indexes
RewriteEngine On
RewriteBase /
# remove index
RewriteRule (.*)/index$ $1/ [R=302]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301,L]

Add .php To Access a File Without Redirecting

Options +FollowSymLinks -MultiViews -indexes
RewriteEngine On
RewriteBase /
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1.php [L]

Was this answer helpful?

Also Read

Configure PHP version

To change the PHP version you need to log in to your cPanel account.How do I access cPanel?At the...

How long does it take for the account activate?

Your account will be active within minutes of your payment confirmation.

cPanel access

To access the cPanel control panel you must log in to your Net Solution client account and go to...

How do I access cPanel (and site) using the temporary URL

In the following, we'll explain how to access the cPanel control panel using a temporary URL.You...

Do I have access to a control panel for my hosting account?

We offer access to  cPanel, a very popular and easy to use control panel that will help you...