Hosting - Guide e FAQ search
clear

Examples of Apache mod_rewrite

The mod_rewrite module is probably the most popular and used in the web field under the Apache server: it allows to manage a website's URL at its best, customizing several options and it's useful for the indexing of contents on search engines. The most popular way to access the setting of this server's  component stands in modifying the .htacess file (the dot suggests an hidden file, usually accessible and adjustable also using FTP) that has to be placed in the  /public_html or /www. folder.

Here is a list of examples on how to configure your own website, according to your needs. You must know that not all the configurations work in all servers and for all websites typologies or CMS and that, if you combine several parts of different examples - such as two different redirects that have two work on the same website- it won't be enough to apply a "copy and paste" but it will be required to blend the two configurations to form a single folder with valid syntax (to learn more take a look at the official documentation).

#specify the website's default page 
DirectoryIndex miaindex.html

 

#redirect from alfa.html to beta.php

RewriteEngine on

RewriteRule ^/?alfa\.html$ beta.php [L]

#redirect from all files with .html extensions to all files with .php

# or : “change” or disguise the files' extension 

RewriteEngine on

RewriteRule ^/?([a-z/]+)\.html$ $1.php [L]

#hides the .PHP extension from the file's name

RewriteRule ^/?([a-z]+)$ $1.php [L]

#redirect SEO-friendly

#from http://mywebsite.com/show.php?country=ITALIA&region=Calabria&city=Cosenza

to http://mywebsite.com/ITALIA/Calabria/Cosenza

 

RewriteRule ^/?([a-zA-Z_]+)/([a-zA-Z_]+)/([a-zA-Z_]+)$ show.php?country=$1&region=$2&city=$3 [L]

 

#only the following IPs can access your website
allow from 64.95.219.140
allow from 210.23.45.67

 

# global Redirect (of all pages) from old.com or www.old.com to new.it
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.old.com$ [OR]
RewriteCond %{HTTP_HOST} ^old.com$
RewriteRule ^(.*)$ http://www.new.it/$1 [R=301,L]

 

#avoid the subdomain1 to be accessable and re-direct automatically to the main domain 
RewriteCond %{HTTP_HOST} ^subdomain1\.main-domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.subdomain1\.main-domain\.com$
RewriteRule ^(.*)$ "http\:\/\/www\.main-domain\.com\/$1" [R=301,L]

 

# Do not allow the prefix .www
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?mywebsite\.it)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]

 

# Always use the prefix .www
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z.]+)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.%1mywebsite.it%{REQUEST_URI} [R=301,L]

 

# As the website opens define a default sub-directory 
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ /directory-default/ [R=301,L]
</IfModule>

 

# Redirect from an old folder to a new one 
RewriteEngine on
RewriteRule ^/?old([a-z/.]*)$ /new$1 [R=301,L]

 

# Always use the protocol https (SSL)
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mywebsite.it/$1 [R=301,L]

 

# block traffic from unwanted refers
RewriteEngine on
Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} spamwebsite1\.com [NC,OR]
RewriteCond %{HTTP_REFERER} searchengine2\.com [NC,OR]
RewriteCond %{HTTP_REFERER} forumspam3\.com [NC]
RewriteRule .* - [F]

 

# type the not allowed files on the server 
RewriteEngine on
RewriteRule .*\.(jpg|jpeg|gif|png|bmp|exe|swf)$ - [F,NC]

 

# avoid images hotlinking 

RewriteCond %{HTTP_REFERER} !^$

RewriteCond %{HTTP_REFERER} !^http://(www\.)?mywebsite\.it/ [NC]

RewriteRule \.(gif|jpg|png)$ - [F]

 

 

 

Did you find this answer helpful?

Still need help?

Contacts

Contacts

Request information, assistance or quotes

Chat

Chat

Ask about services and promotions

Ticket

Ticket

Submit a request for technical support

Payments

Payments

Make a payment in a few clicks