Apache mod_rewrite and Examples

Last modified: January 18, 2025
You are here:
Estimated reading time: 1 min

What is mod_rewrite?

BrawHosting utilizes Apache, the open-source HTTP server software, to host your website. Apache can be customized using modules, and one such module is mod_rewrite. This module enables you to redirect URLs, rewrite requested URLs, restrict access to your site, and much more.

You can invoke rewrite rules by placing them in your .htaccess file. Below are examples showing how these rules can be used to customize your hosting experience.

mod_rewrite Examples

Here are examples of rules that allow you to do the following:

Set Default Homepage

DirectoryIndex home.html

Specify IP Access

deny from all
allow from 64.95.219.140
allow from 210.23.45.67

Redirect All Website Pages

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^olddomain.com$
RewriteRule ^(.*)$ https://www.newdomain.com/$1 [R=301,L]

Prevent Sub-folder Access

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

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

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?example\.com)$ [NC]
RewriteRule .? https://%1%{REQUEST_URI} [R=301,L]

Force www Domain Access

RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z.]+)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? https://www.%1example.com%{REQUEST_URI} [R=301,L]

Set Default Home Folder

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ /folder/ [R=301,L]
</IfModule>

Redirect Folder Access

RewriteEngine on
RewriteRule ^/?old([a-z/.]*)$ /new$1 [R=301,L]

Force HTTPS Access

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

Block Multiple Referrers Traffic

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

RewriteEngine on
RewriteRule .*\.(jpg|jpeg|gif|png|bmp|exe|swf)$ – [F,NC]

Remove Index

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

Remove .php

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

Remove Index and /

Options +FollowSymLinks -MultiViews -indexes
RewriteEngine On
RewriteBase /
RewriteRule (.*)/index$ $1/ [R=302]
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 /
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1.php [L]

Was this article helpful?
Dislike 0
Views: 32

Braw Hosting Sales

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.

Scroll to Top