The Elegance of .htaccess

.htaccess file can add some elegance to what can be sometimes long and clumsy urls. It must be written as text file (not forgetting the preceding full stop) and generally saved in the root directory of your domain with no suffix.


LEARN .htaccess

An excellent resource for getting to grips with .htaccess…
2020 Complete Guide to .htaccess

Tidy short Url’s without the suffix

Options -Indexes

RewriteEngine on

RewriteCond %{HTTP_HOST} ^$
RewriteRule ^(.*)$$1 [R=301,NC,L]

RewriteRule ^$ /home [R=302,NC,L]

RewriteRule ^home$ index.php [NC,L]
RewriteRule ^htaccess$ htaccess.php [NC,L]

The above file is designed to rename the page url’s from index.php to home etc.

Look at the url in the browser now! You should see a neat url
Not having a suffix of i.e. .php or .htm etc.

NB. With your .htaccess file in place remember to rename each of your hyperlinks in your website from ‘index.php’ to ‘home’ etc. Otherwise the hyperlinks will point directly to your .php files and load them so you won’t see your neat abbreviated urls.

[The square bracketed codes]

[NC] No Case. Use of the [NC] flag causes the RewriteRule to be matched in a case-insensitive manner. That is, it doesn’t care whether letters appear as upper-case or lower-case in the matched URI.

[L] Last. Use this flag [L] to indicate that the current rule should be applied immediately without considering further rules. Similar to what ‘;’ does for a single PHP statement or } for a block statement.

.htaccess Rewrite Rules that you should know

My example above is for using .htaccess to provide elegant Url’s. But the primary function of the .htaccess file is Redirection.

A handy website offering 10 things you should know about .htaccess Rewrites is 10 Mod Rewrite Rules you should know.

In the Rewrite rule below any request for an image file will be proxied to your dedicated image server. The match is case-insensitive, so that .jpg and .JPG files are both acceptable.

RewriteRule (.*\.(jpg|gif|png))$$1 [P,NC]

Use of the [P] flag implies [L] - that is, the request is immediately pushed through the proxy, and any following rules will not be considered.

You must make sure that the substitution string is a valid URI (typically starting with http://hostname) which can be handled by the mod_proxy. If not, you will get an error from the proxy module. Use this flag to achieve a more powerful implementation of the ProxyPass directive, to map remote content into the namespace of the local server.

Apache HTTP Server Version 2.2
Rewrite Rules

Flag definitions

Redirect from .com to or to com


.htaccess Rewrite conditions (RewriteCond RewriteRule)

Scenario: We want direct visitors to our .com website to our website or vice versa.

          Options -Indexes

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^(www\.)?yourwebsite\.com$ [NC]
RewriteRule ^(.*)$$1 [R=302,L]


With this .htaccess code anything with “” in the uri gets replaced with “” this means that even “” will redirect nicely to “” etc.

The above .htaccess Rewrite Condition Rewrite Rule code is set to [R=302,L] (Rewrite R=302 which is the default for Rewrite Rules so could in this instance be left out. This code No. denotes a “Temporary” Redirect.

Types of .htaccess Redirects and what they do

300, 301, 302, 303, 307, and 308

300 Multiple Choice

The request has more than one possible set of responses. User-agent or user should choose one of them. There is no standardized way to choose one of the responses.

301 Permanent Redirect

Remove a page completely but forward to another page.

302 Temporarily Redirected

You may wish to move a URL temporarily to another URL without upsetting the listing & SEO page rank within search engine results as you intend to move the URl back to it’s original location in the near future

Additionally: Prevents bookmarking or refreshing of pages that hold one-time only data like credit card payment information (Any/all information entered within a form). The server may well be set-up to redirect you to another page and carry across the submitted details.

NOTE: The difference between 301 and 302 Redirection.
When a web crawler encounters a link to your site whose URL is configured with a permanent redirect, your (Apache) web server responds with a 301 status code and then redirects the crawler to the new URL. The search engine not only accepts the redirection to the new URL but also begins the process of transferring any existing page rank value from the old URL to the new one. It’s the second part of this process that’s so important for SEO which simply isn’t done with a 302 Rewrite Rule.

303 Temporary Redirect

Redirection and transferring the data from a form input across to the newly redirected page. Information sent using the POST method. Information retrieved using the GET method.

307 Temporary Redirect

Identical to that of 303 but without changing the way the information was transferred from one technique to another! The same technique is used.

308 Permanent Redirect (experimental)

Same as 307 but set permanently

Redirect code definitions

“300” Rewrite code definitions from Mozilla org.

LEARN .htaccess

An excellent resource for getting to grips with .htaccess…
2020 Complete Guide to .htaccess



Please share if you find the content useful - thank you

Master Template World


Master Template Forum Robot


Have a comment or something to say?
Say it here…

[privacy policy]