Htaccess beginner guide to setup seo friendly url for your website

Here in this tutorial we’ll learn about .htaccess properties and how to setup .htaccess file in our project for custom error pages handling and create seo friendly url.

Here i am going to you aware about the .htaccess power and why it is important for you php based project using this file you can easily redirect normal apache server file system in custom urls which is most useful for seo purpose.

First of all we need to know what is “.htaccess” ?
htaccess is short for Hypertext Access, and is a configuration file used by Apache based web servers, When a .htaccess file is placed in a directory which is in turn ‘loaded via the Apache Web Server’, then the .htaccess file is detected and executed by the Apache Web Server software.

Time to setup .htaccess file.

Create filename “.htaccess” and save it to your project root directory.
Note: By default this file in hidden format please change your system settings and display hidden files to view it.
Note: Before executing this file please make sure that you have successfully enabled mod_rewrite extension in php.ini file

Task-1:- We have to handle error page request by .htaccess like if someone requesting for 404,500 type of request/errors.

These are some of the most common errors:
401 – Authorization Required
400 – Bad request
403 – Forbidden
500 – Internal Server Error
404 – Wrong page

To handle these request/errors follow below code.

# Error pages handling
errorDocument 401 http://www.youwebsite.com/error_401.html
errorDocument 400 http://www.youwebsite.com/error_400.html
errorDocument 403 http://www.youwebsite.com/error_403.html
errorDocument 500 http://www.youwebsite.com/error_500.html
errorDocument 404 http://www.youwebsite.com/error_404.html

Each time if someone request for wrong page(404) he will be automatically redirected to error_404.html page, You can customize these pages as per you need.

If you want to disable whole directory then put below code in your .htaccess file

# Disable directory browsing, Nobody directly browse your directory. 
Options All -Indexes



Task-2:- Create your awkward shaped urls into seo friendly urls using .htaccess
Suppose you have these 3 types of urls in your project.

http://www.example.com/profile.php?uid=12345
http://www.example.com/profile.php?username=rohit
http://www.example.com/search.php?q=delhi

Need to convert into seo friendly url like

http://www.example.com/profile/12345
http://www.example.com/profile/rohit
http://www.example.com/search/delhi

Create .htaccess file and write below command.

.htaccess

Options +FollowSymLinks
RewriteEngine On 
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
 
RewriteRule ^profile/([0-9]+)$ ./profile.php?uid=$1
RewriteRule ^profile/([a-zA-Z0-9_-]+)$ ./profile.php?username=$1
RewriteRule ^search/([a-zA-Z0-9_-]+)$ ./search.php?q=$1

Handle two and three parameters

http://www.example.com/profile.php?username=rohit&page=about
http://www.example.com/search.php?q=delhi&page=1&total=100

Need to convert into seo friendly url like

http://www.example.com/profile/rohit/about
http://www.example.com/search/delhi/1/100

For doing this your .htaccess file will be

.htaccess

Options +FollowSymLinks
RewriteEngine On 
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
 
RewriteRule ^profile/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ profile.php?username=$1&page=$2
RewriteRule ^search/([a-zA-Z0-9_-]+)/([0-9]+)/([0-9]+)$ profile.php?q=$1&page=$2&total=$3

This is the just basic and quick understanding tutorial of Htaccess you can study more features of htaccess like password protected directory, speed up website load time many more from here http://www.htaccess-guide.com/

If you like this post, Please don’t forget to subscribe My Public Notebook for more useful stuff.

Posted in SEO