Home > Help Center > Shared Hosting > Website > Mod_rewrite in a Nutshell

Mod_rewrite in a Nutshell

mod_rewrite provides a way to modify incoming URL requests dynamically, based on regular expression rules. If the incoming URL triggers a rule in mod_rewrite, the pattern is replaced by the string or action you desire. This operation transforms the links in your website by user-friendly URLs.

If you need a custom configuration for your application on a shared web hosting server, where you don’t have access to the server's global configuration files, mod_rewrite is the way to get the job done.

Mod_rewrite Advantages

Apparent advantages of using mod_rewrite are increased page security and better search engines visibility.

Because mod_rewrite conceals the file structure as well as the content’s location, the effect of using it is increased site security.

Mod_rewrite makes offers better indexing of the pages by replacing the dynamic incomprehensible part of the URL with key phrases comprehensible to the search engines:

  • http://www.hostpulsedemo.net/products.php?id=123&cat=12
  • http://www.hostpulsedemo.net/product/category/

Let's take a look at some of the most commonly used mod_rewrite examples.

Force the use of www.hostpulsedemo.net instead of hostpulsedemo.net

For sites running on a port other than 80:

RewriteCond "%{HTTP_HOST}"   "!^www\.hostpulsedemo\.net" [NC]
RewriteCond "%{HTTP_HOST}"   "!^$"
RewriteCond "%{SERVER_PORT}" "!^80$"
RewriteRule "^/?(.*)"        "http://www.hostpulsedemo.net:%{SERVER_PORT}/$1" [L,R,NE]

For sites running on port 80:

RewriteCond "%{HTTP_HOST}"   "!^www\.hostpulsedemo\.net" [NC]
RewriteCond "%{HTTP_HOST}"   "!^$"
RewriteRule "^/?(.*)"        "http://www.hostpulsedemo.net/$1" [L,R,NE]

If you wanted to do this generically for all domain names that are configured in the hosting account, use the following code:

RewriteCond "%{HTTP_HOST}" "!^www\." [NC]
RewriteCond "%{HTTP_HOST}" "!^$"
RewriteRule "^/?(.*)"      "http://www.%{HTTP_HOST}/$1" [L,R,NE]

To make these rulesets work place .htaccess file in the DocumentRoot of the hosting account.

Use the domain name hostpulsedemo.net without www:

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

Moved DocumentRoot

Redirect visitors from homepage to subdirectory /about/ using the code below:

RewriteEngine on
RewriteRule   "^/$"  "/about/"  [R]

Note that this can also be handled using the RedirectMatch directive:

RedirectMatch "^/$" "http://hostpulsedemo.net/about/"

Use meaningful URLs with mod_rewrite

For example, instead of URL http://www.hostpulsedemo.net/products.php?id=123&cat=12 you can use URL http://www.hostpulsedemo.net/product/category/

RewriteEngine on
RewriteRule ^ product/category$ /products.php?id=123&cat=12

Redirect all pages from olddemo.com to newdemo.com:

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

Prevent hotlinking for all other domains except hostpulsedemo.net:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?hostpulsedemo.net(/)?.*$ [NC]

Protect only the specified file extensions:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?hostpulsedemo.net(/)?.*$ [NC]
RewriteRule .*.(gif|jpe?g)$ [F,NC]

Removing file extensions (.php and .html)

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

Conclusion

Mod_rewrite is a powerful tool that allows you to have friendly and simple URLs in your website.

We have covered the most common uses of mod_rewrite in this article, but you can find additional information and examples in the official Apache documentation regarding the module at the following link: [modrewrite documentation](http://httpd.apache.org/docs/current/mod/modrewrite.html)

#settings

Still not finding what you're looking for?

Contact our support team with any additional questions or concerns.

Contact support