Htaccess tutorial and tips

In this tutorial, we are basically going to learn about .htaccess file.

Before you start, you should have a basic knowledge of Regular Expression.

Here we don’t discuss Regular Expression. So I assumed that you already know about Regular Expression –


What is .htaccess file ?

.htaccess (hypertext access) is a configuration file. By using this file you can easily configure the Apache web server, such as you can enable or disable additional functionality and features of the Apache Server.


How to use .htaccess file ?

It is simple to use, just create a .htaccess file on your website’s root directory and it will be automatically detected and executed by the Apache web server. Note that it is not a file extension.


1. Handling Error documents

ErrorDocument [error_code] [path of the file which you want to show]

ErrorDocument 401 /error_pages/401.html
ErrorDocument 404 /error_pages/404.html
ErrorDocument 500 /error_pages/500.html

2. Redirects

Redirect [old path or url] [new path or url]
Redirect the index.html file to index2.html

Redirect /index.html /index2.html

3. Use regular expressions to redirect

If you want to use Regular Expressions to redirect to another path, use RedirectMatch

RedirectMatch "^/index\.html/?$" "/index.php"

4. Redirect non-existing pages to the specific page

If a visitor tries to reach a page that does not exist, then you can redirect the visitor to a specific page by using the following code.

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

5. Adding www in the URL

You can easily redirect your non-www domain or URL to www by adding the following code.

http://example.com/
to
http://www.example.com/

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

6. Removing www in the URL

You can easily remove www on your domain or URL by using the following code.

http://www.example.com/
to
http://example.com/

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]

7. Attaching https in the URL

Add https on your domain or url by using the following code.

http://example.com/
to
https://example.com/

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

8. Detaching https in the URL

Remove https from your domain or url by using the following code.

https://example.com/
to
http://example.com/

RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

9. Make SEO Friendly URL

By using a .htaccess file you can easily make an SEO friendly URL. Let’s have a look –

1 – Example

http://www.example.com/index.php?name=John
to
http://www.example.com/name/John

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^name/([a-zA-Z0-9_-]+)$ index.php?name=$1 [NC,L]

2 – Example

http://www.example.com/index.php?name=John&age=19
to
http://www.example.com/name/John/19

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^name/([a-zA-Z0-9_-]+)/([0-9]+)$ index.php?name=$1&age=$2 [NC,L]

3 – Example

http://www.example.com/home-page.html
to
http://www.example.com/home

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^home/?$ home-page.html [NC,L]

10. Remove file extension

http://www.example.com/about.html
to
http://www.example.com/about

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

Tip – If you want to remove the PHP file extension, then just remove .html and add .php


Note:

Write rewrite rules all the time inside the <IfModule mod_rewrite.c></IfModule> tag.

Because if your rewrite feature is disabled on your Apache server, it will turn on that feature, otherwise you can get Error.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
</IfModule>

Posts Navigation


Leave a Reply

Your email address will not be published. Required fields are marked *