Monday, December 2, 2013

.htaccess is one of those things that people often refer to as a dark art, much like reincarnation. However it shouldn't be overly confusing or hard, so here we'll look at the basics of rewrite rules with htaccess as well as the more important 301 redirects. Before you take a read, it's advisable that you take a brief read through the Introduction to Regular Expressions as these will feature rather heavily within rewrites etc.

ReWrite URL:
The are simple rewrites, not redirections. This will not change the URL that a user sees within the address bar but will load the page that you tell it to.
The rules are split into two sections, the first one being what the user/visitor will enter in their browser, and the second being the page that will be served to the user instead

Note: .htaccess file will be in hidden format, please change your folder and file settings to view this file. 

How to Create a .htaccess File?
Open any text editor application and file save as with .htaccess name and enablemod_rewrite extension in php.ini file in Apache Web Server configurations. 

Disable directory Listing
If you want to disable folder files listing, include following code. 
# Disable Directory Browsing
Options All -Indexes

Error Pages
Here error page is redirecting to error.html
errorDocument 400
errorDocument 401
errorDocument 404
errorDocument 500

RewriteEngine On it is turn on Rewrite Rules in Apache Server. if you want to turn off, just change the value to off.
RewriteEngine on

Domain Redirection
.htacces code for redirecting to
RewriteCond %{HTTP_HOST} ^
RewriteRule (.*)$1 [R=301,L]

Sub Domain Redirection
Sub domain redirection mapping to folder. Here is connecting to website_folder folder. 
RewriteCond %{HTTP_HOST} ^www\.yourwebsite\.com$
RewriteCond %{REQUEST_URI} !^/website_folder/
RewriteRule (.*) /website_folder/$1

Here is connecting to subdomain_folderfolder. 
RewriteCond %{HTTP_HOST} ^subdomain\.yourwebsite\.com$
RewriteCond %{REQUEST_URI} !^/subdomain_folder/
RewriteRule (.*) /subdomain_folder/$1

Htaccess File Inside The Folder.

Old Domain Redirection
htaccess code for redirecting old domain( to new domain( Live demo is now redirecting to
RewriteCond %{HTTP_HOST} ^
RewriteRule (.*)$1 [R=301,L]

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

Friendly URLs
Friendly/Pretty URLs help in search engine rankings.

Profile URL 
Profile parameter allows [a-zA-Z0-9_-] these inputs.

RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?username=$1

Messages URL

RewriteRule ^messages/([a-zA-Z0-9_-]+)$ messages.php?message_username=$1
RewriteRule ^messages/([a-zA-Z0-9_-]+)/$ messages.php?message_username=$1

Friends URL
RewriteRule ^friends/([a-zA-Z0-9_-]+)$ friends.php?username=$1
RewriteRule ^friends/([a-zA-Z0-9_-]+)/$ friends.php?username=$1

Friends URL with Two Parameters 
Here the first parameter allows [a-zA-Z0-9_-] and second parameter allows only number [0-9]

RewriteRule ^friends/([a-zA-Z0-9_-]+)/([0-9]+)$ friends.php?username=$1&page=$2
RewriteRule ^friends/([a-zA-Z0-9_-]+)/([0-9]+)/$ friends.php?username=$1&page=$2

Hiding File Extension
RewriteRule ^([^/.]+)/?$ $1.html

What if...
... we don't want to rewrite something if the file already exists, or it's a directory that exists? So for example we have an about directory that already exists which contains a load of files - so anyone going to the directory should be served the directory, not our rewrite.
We can look now to use things called Rewrite Conditions, these allow us to set rules for when the rewrite rule will take effect, the below example will check that the requested URI isn't a directory or file that exists already:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

Rewriting Flags:

There are some handy flags that we can append to the end of rewrite rules that allow us to specify extra additions, so for example we can specify that any additional query string that is sent in the request is also sent through to the script we're loading. Or we can tell the redirect that this is the last redirect that it should do if it matches - this could solve some unintended rewrite side effects.

There are loads of flags that you can use, but we'll cover just one or two:

QSA: This will append the query string that was sent to the original page to the end of any query string that you had before. eg: /about?foo=bar will become /about.php?foo=bar.
R: This will cause a HTTP redirect to be issued to the browser, you can optionally add a redirect code as well - we will cover this later in this article.
L: This will stop mod_rewrite from running any further rules within the set, and run the matched rule with this flag.
F: Final simple one, this will return a 403 forbidden error to the client, this insinuates a flag of L also.
You can use a flag by putting the letter in square brackets after the rewrite rule, so for example:

RewriteRule ^([0-9a-z]+)$ index.php?page=$1 [QSA]

This will rewrite the following:
/about -> index.php?page=about
/bacon?foo=bar -> index.php?page=bacon&foo=bar


A very brief and simple introduction to .htaccess, hopefully entitling you to take what you have now and implement neater, simpler URLs but also meaning that you gracefully, and respectfully redirect people to other locations on your site as you change page names, slugs or even domains.
Just ensure that you test your htaccess files before you shove them live, a '500 Internal Server' error is in most cases a sign that there might be an issue with your config.

Ashutosh Kumar

I am A Web Developer, Web Designer And I Love the Computers and Web Technology .


Do Not Spam

Search PC Tips