Learn more about .htassess file

The .htaccess file is a general text file, which can be create using Notepad or any text editor. This file  contains the configuration statements/ commands to customize the Apache Web server as per user requirement.

How to create a .htaccess file?

if you try to create a .htaccess file in Windows it will not allow you to do so because the file won't have a name. To solve this problem you can create a file called htaccess.txt then transfer it to your web server and rename it to .htaccess. Then you can download the file, edit save and re upload.
or Very simple method open any editor like notepad go to file save within double quotations write ".htacess" the file will be created.
Also you can do it through command prompt.
Open Command Window (window key + r) type "command" press ok.
Now change to root directory. CD\ + enter
Type: copy con .htaccess press enter. Now press ctrl + z to save the file.
You can check if your file is created or not.

What are the use of .htaccess file?

.htaccess file helps you to Password protect you website, Deny/ Allow remote hosts or IP from your web site, Protecting your images and other files from linking, Add Security to your PHP projects, URL redirect/rewrite etc.

URL redirect using the .htaccess file

1. Add www by default to your URL.

301 redirect forcing all http requests to redirect from domain.com to www.domain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

2. Remove www by default from your URL.

301 redirect forcing all http requests to redirect from www.domain.com to domain.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^/?$ "http\:\/\/domain\.com\/" [R=301,L]

3. Redirect from domain.com to domain.com/index.html

If you want to redirect all incoming URLs from domain.com/ to domain.com/index.html then use the following code.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^$ http://example.com/index.php [L,R=301]

4. How to add a trailing slash

Like Yahoo some search engines remove the trailing slash from URLs. So it creates duplicated content problems when the same page content is accessible under different URLs. Per example domain.com/google/ will index in Yahoo as domain.com/google - which would result two different URLs with the same content.
To solve this problem lets add a trailing slash to the URLs
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !example.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://example.com/$1/ [L,R=301]

5. How to remove a trailing slash

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} (.*)$
RewriteRule ^(.+)/$ http://www.example.com/$1 [R=301,L]

6.Redirect visitors from old site to a new site

RewriteEngine On
RewriteCond %{HTTP_HOST} !oldexample.com$ [NC]
RewriteRule ^(.*)$ http://www.newexample.com/$1 [L,R=301]


Comments
comments powered by Disqus