Saturday, December 23, 2017

Htaccess File Tutorial and Tips

Today we are going to share some ideas about Htaccess. Basically Htaccess is a configuration file use on web servers running the Apache Web Server software and Using Htaccess you can easily configure and redirect Apache Web Server file system.

What is .htaccess ?

.htaccess is a configuration file for use on web servers running the Apache Web Server software. 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. These .htaccess files can be used to alter the configuration of the Apache Web Server software to enable/disable additional functionality and features that the Apache Web Server software has to offer. These facilities include basic redirect functionality, for instance if a 404 file not found error occurs, or for more advanced functions such as content password protection or image hot link prevention.

How to Enable .htaccess in Xampp Application

Once you have installed Xampp application in your system, all the configuration file should be available with you in your system. Here we have installed Xampp application in D: Drive.

Lets follow the below steps to enable the .htaccess :
1. Open the below directory in you system :
     D:\Xampp\apache\conf
   

2. Then Open httpd.conf File using text editor and search for below line :   
LoadModule rewrite_module modules/mod_rewrite.so

 and Remove hash form the that line and save that file after that restart your Apache server and reload that page in browser.

How to Create a .htaccess File?

Open any text editor application and file save as with .htaccess in root directory folder. Check the below screenshot for more reference.

Lets see the few basic operations for .htaccess configuration file.

Default directory Listing

This is the default directory listing for Xampp application, no restriction is here.



Disable directory Listing

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

Error Pages Redirection 

If you enter invalid Link, then it will redirect to error.html page. For Error Page redirection, you require following code :

errorDocument 400 http://localhost/skptricks/demo/error.html
errorDocument 401 http://localhost/skptricks/demo/error.html
errorDocument 404 http://localhost/skptricks/demo/error.html
errorDocument 500 http://localhost/skptricks/demo/error.html

Entering invalid link in address bar :
Page redirected to error.html page :
NOTE : To use Rewrite Rules feature in Apache Server, you need to turn on the RewriteEngine. if you want to turn off, just change the value to off. Mention the below statement in .htaccess file.

Example : 
RewriteEngine on
RewriteEngine off

You need to turn on the RewriteEngine for the below examples, Otherwise it will not work.

Domain Redirection

.htacces code for redirecting yourwebsite.com to www.yourwebsite.com 


RewriteCond %{HTTP_HOST} ^yourwebsite.com
RewriteRule (.*) http://www.yourwebsite.com/$1 [R=301,L]

Sub Domain Redirection

Sub domain redirection mapping to folder. Here http://www.yourwebsite.com is connecting to website_folder folder.

RewriteCond %{HTTP_HOST} ^www\.yourwebsite\.com$
RewriteCond %{REQUEST_URI} !^/website_folder/
RewriteRule (.*) /website_folder/$1

Similarly we can link to sub-domain folder.
Here http://subdomain.yourwebsite.com is connecting to subdomain_folder folder.

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

Profile URL

Profile parameter allows [a-zA-Z0-9_-] these inputs and here we have used regular expression statement.
we are apply rewrite rules to below URL.
http://localhost/skptricks/demo/profile.php?username=skptricks
to
http://localhost/skptricks/demo/skptricks

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


Post URL 

Similarly we are using rewrite rules in another URL. Which will rewrite the URL in below format.
http://localhost/skptricks/demo/post.php?title=html
to
http://localhost/skptricks/demo/post/html


RewriteEngine on
RewriteRule ^post/([a-zA-Z0-9_-]+)$ post.php?title=$1
RewriteRule ^post/([a-zA-Z0-9_-]+)/$ post.php?title=$1


Forum URL With Two Parameter 

Similarly we are using rewrite rules in another URL. Which will rewrite the URL in below format.
http://localhost/skptricks/demo/forum.php?year=2017&title=UFT
to
http://localhost/skptricks/demo/forum/2017/UFT

RewriteEngine on
RewriteRule ^forum/([0-9]+)/([a-zA-Z0-9_-]+)$ forum.php?year=$1&title=$2
RewriteRule ^forum/([0-9]+)/([a-zA-Z0-9_-]+)$ forum.php?year=$1&title=$2


Hiding File Extension

If you want to hide the webpage extension then use the below code :
http://localhost/skptricks/demo/post.php
to
http://localhost/skptricks/demo/post

RewriteRule ^([^/.]+)/?$ $1.php

Video Link :


Download Link  : https://github.com/skptricks/php-Tutorials/tree/master/Htaccess%20File%20Tutorial%20and%20Tips

Do Comments in case of any questions...

No comments:

Post a Comment