Things to do with .htaccess codes

by Saad Irfan

Search Engine Optimization matters a lot in every website, and keeps your sites on the top from the WWW pool of same sites. With the help of .htaccess you can easily redirect your visitors, hide page extensions and can increase the site security. Not only this, .htaccess also helps you a lot to make your web page’s link more search engine friendly which is need of every business. In this article we will cover some of most demanding .htaccess codes which you might be looking for, from long time.

I have been working in the SEO field from last 2 years, and these .htaccess codes always helped my projects to gain elevated ranking on search engines.

Hide Original Extension of the Page

You can hide your original page extension with the help of URL rewriting, easily, depends on the security restrictions from your host.

Replace the .htaccess from root folder of your website directory with the new .htaccess file having this script.

Options +FollowSymlinks

RewriteEngine on

RewriteRule ^(.*).htm$ $1.php [nc]

To understand this script, suppose you are having file contact.php on server, you can access this page by writing contact.html also. Means you don’t have to expose the original extension of your page. However [nc] just make sure that it’s not a case sensitive.

Make the URL friendlier

We will discuss tips for making URL easy to read by human and friendlier for search robots.

In case you are running a social community or shopping web site, you might be having large number of members or products. And you will also wish to make your every member’s or product’s page more search friendly and understand by human.

Options +FollowSymlinks

RewriteEngine on

RewriteRule ^ display-([0-9]+).php$ display.php?id=$1

Instead of using “display.php?id=1” for calling member “1”, you can access page with “member-1.php”, which is evidently straightforward to interpret by human and friendly to Search robots, as they also like sophisticated links.

In case you want to implement the same method for accessing page with the Name of Member or Product, you can’t do it with this code. The reason of mentioning this code was to filter more and more inputs for security reasons, as it will accept value of variable “id” only 0 to 9.

RewriteRule ^ display-(a-zA-Z0-9_-).php$ display.php?id=$1

By replacing the last with this line, you can give input of alphabets also along diigts.

You might have also noticed, some of the famous sites use short URL for their members’ profiles and use the Name as a substitute of using numerical ID.

Let suppose my Member id on a domain is “35” and my User Name is “saadirfan”. The site lets the user access my profile with the following link

“example.com/saadirfan”

Instead of

“example.com/member.php?name=saadirfan”

The code for this mystery is

RewriteEngine On

RewriteRule ^([a-zA-Z0-9_-]+)$ member.php? name=$1

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

The second line of the script make sure that page still opens in case visitor type “example.com/saadirfan/” instead of “example.com/saadirfan”.

Redirect your Visitors to Notice Page

Don’t lose your visitors while having some maintenance work on your site. Redirect your visitors to your Notice Page to tell them the reason of downtime of your site, or ask them to subscribe to have update via email about re-launching of site. The code given below will take all the users to notice.html

Options +FollowSymlinks

RewriteEngine On

RewriteBase /

RewriteCond %{REQUEST_URI} !/ notice.html$

RewriteRule .* /notice.html [R=307,L]

Redirect Visitors to Fancy Custom Error Pages

Sick with style of default custom error pages? Here is the way to redirect your visitors to your own made Custom Error Page, in case of having any error. Fancy Error Pages with colorful sorry pictures can help you to overcome the anger of the visitors as no user wants to see Error Page while accessing any URL on web.

ErrorDocument 400 /errors/400.html

ErrorDocument 401 /errors/401.html

ErrorDocument 403 /errors/403.html

ErrorDocument 404 /errors/404.html

ErrorDocument 500 /errors/500.html

Now place the pages with the name of 400.html, 401.html, 403.html, 404.html and 500.html and you will be good to go.

Prevent IP Range or a IP

Sick from Robotic attempts on your server, or facing security breach attempts from any IP? Just block the IP range or Specific IP address.

order allow,deny

deny from xxx.xxx.xxx.xx

deny from xxx.xxx.xxx.

allow from all

Confused?

order allow,deny

deny from 123.45.6.7

deny from 012.34.5.

allow from all

The script prevents the access to the site for IP 123.45.6.7, and from any range under this IP. Such as 012.34.5.1, 012.34.5.2, 012.34.5.3, 012.34.5.4, ……

Please use these codes on your own liability. There might be some restrictions on implementing some htaccess codes on your server, from your hosting provider. Your file also might be automatically get deleted and replaced with default files, depends on hosting provider’s policies. Please confirm from the service provider before proceeding.