Create .htaccess File

Advertisements

Prev Tutorial Next Tutorial

How to Create .htaccess File

.htaccess file is a configuration file for use on web servers running the Apache Web Server software. Using .htaccess file we can do following things;

  • Block bots
  • Block or Allow an IP
  • Resolve Canonical Issue
  • Prevent Directory Listing
  • Prevent viewing of .htaccess file
  • Manage Custom Error Pages (400, 404, 500, etc)
  • Redirect File or Directory to specific URL
  • Set default page to display when a directory is accessed

Redirect WWW to non-WWW

RewriteEngine on
rewritecond %{http_host} ^sitesbay.com [nc]
rewriterule ^(.*)$ http://sitesbay.com/$1 [r=301,nc]

Redirect non-WWW to WWW

RewriteEngine on
rewritecond %{http_host} ^sitesbay.com [nc]
rewriterule ^(.*)$ http://www.sitesbay.com/$1 [r=301,nc]

.htaccess: This file is mainly used for redirect url, to create this file you can create any file and save with .htaccess file name.

Note: You can also redirect using cpanel.

remove using cpanel

Custom Error Pages for Better SEO

One use of the .htaccess file is to redirect users to a custom error page depending on the specific web server error they encounter.

.htaccess file for Custom Error Pages

ErrorDocument 404 /filenotfound.html

.htaccess file for Custom Error Pages

//Custom 400 errors
ErrorDocument 400 /error400.html

//Custom 401 errors
ErrorDocument 401 /error401.html

//Custom 403 errors
ErrorDocument 403 /error403.html

//Custom 404 errors
ErrorDocument 404 /error404.html

//Custom 500 errors
ErrorDocument 500 /error500.html

If you wanted to redirect users for another error, such as 500, Internal Server Error, you would do it like this (and so on)

.htaccess file for Custom Error Pages

ErrorDocument 500 /filenotfound.html

DirectoryIndex Uses

Using directoryindex command we can specify a default page to display when a directory is accessed.

DirectoryIndex Uses

DirectoryIndex index.html index.cgi index.php

In above syntax, if there was no file named index.html in your directory, then the server would look for a file called index.cgi. If that file wasn't present, it would look for one called index.php, and so on.

Comment for Htaccess File

Use # for comment in htaccess file

Comment in htaccess File

# BLOCK SITE REFERRERS
RewriteEngine on
 RewriteCond %{HTTP_REFERER} \<\?\=\$block_referrer\?\> [NC,OR]
 RewriteCond %{HTTP_REFERER} http\://www\.studytonight\.com/ [NC,OR]
 RewriteCond %{HTTP_REFERER} http\://www\.pinkduck\.ga/ [NC,OR]

Redirect All Files with Certain Extension

Redirect all .php extension file on .html file

Redirect All .php URL on .html Extension File

RewriteEngine On
RewriteCond %{REQUEST_URI} .php$
RewriteRule ^(.*).php$ /$1.htm [R=301,L]

Redirect all .html Extension File on .php Extension File

Redirect all .html Extension File on .php Extension File

RewriteEngine On
RewriteCond %{REQUEST_URI} .php$
RewriteRule ^(.*).htm$ /$1.php [R=301,L]

Redirect HTTPS non-www to HTTPS www using .htaccess

Redirect HTTPS non-www to HTTPS www using .htaccess

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

For Read more about .htaccess file visit complete Htaccess File Tutorial


Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements