01 logo

What is a .htaccess? How to create and use the .htaccess file?

Introduction, Usage, Creation and Examples

By Elayer For AllPublished 2 years ago 4 min read
What is the .htaccess? How to create and use the .htaccess file? from elayer for all

What is a .htaccess?

.htaccess is a file, which is a powerful website file that controls the high-level configuration of your website. On servers that run Apache (a web server software), this .htaccess file allows making changes to the website's configuration without having to edit server configuration files.

While Apache(https://httpd.apache.org/docs/2.4/howto/htaccess.html) says,

The .htaccess files or distributed configuration files provide a way to make configuration changes on a per-directory basis. A file, containing one or more configuration directives, is placed in a particular document directory, and the directives apply to that directory and all subdirectories thereof.

What is the location of the .htaccess file?

The .htaccess file is located in the root directory of the website. Depending on the hosting provider, the root directory may be a folder labeled with public_html, www, htdocs, or httpdocs. Some hosting providers add it by default while few are not, it is easy to create.

How to create a .htaccess file?

  1. Just open any text editor or IDE
  2. Open a new file, and add content to it.
  3. Click on save and give file name only as .htaccess
  4. Yes, it's only .htaccess without any other extension or name.
  5. Then upload it to the root directory of the site through an FTP client or file manager of the hosting provider.

Why .htaccess?

It helps a lot in SEO that is, Search Engine Optimization.

Almost all websites use this to improve websites SEO technically. This file is a distributed server configuration file for the Apache webserver.

It is widely used

  • to make web page redirections
  • HTTP headers modifications
  • to hide file extensions
  • to prevent access to some directory (path)
  • to hide webpage query params (query parameters).
  • to reduce URL: the method used here is Clean URL
  • manage web crawling etc.

How to comment in a .htaccess file?

To comment in a .htaccess need to use the Hash symbol # at the beginning of the line.

# comment in the .htaccess file

How to redirect via .htaccess?

The .htaccess file enables redirection of the webpage from one page to another page or from an old page or directory to a new page or new directory without having to keep the old page or directory. For example, initially, if the product directory was this http:elayer.epizy.com/product/, after some time developer changed it to http:elayer.epizy.com/p/, if some may try old URL then they will get an Error 404, to prevent it easy write it on .htaccess file.

RewriteEngine On

RewriteRule ^/?product/(.*)$ /p/$1? [R=301,L]

Initially, if the file was product.php, then later in the stage it was renamed to p.php, again using the old link will show Error 404, to prevent that just set up a redirection to the .htaccess file from product.php to p.php.

RewriteEngine On

Redirect /product.php /p.php

Exact procedure in browser(client):

  • On entering the link http://localhost/product.php
  • Status Code will be `302 Found` and the type is `document / Redirect`. It shows failed to load response data: No content available because this request was redirected.
  • Again it will send the request http://localhost/p.php and get response loads as it is. There is no change in status or type because it's a normal request. Status code 200 and type is a document only.

HTTP Error 302:

The HTTP(HyperText Transfer Protocol) Error code 302 found indicates that a specific URL (Uniform Resource Locator) has been temporarily moved to a new URL or location. Whenever users, link visitors, Google robots, Apps, or other search engines access the original URL, 302 redirect delivers an automatic response indicating a new address (maybe path or file name or different URL). The 302 redirects can benefit a website on several occasions.

This can be done by .htaccess rules and this is one option.

Is it possible to add more than one .htaccess file?

Yes, it's possible; but not in the same directory.

We can add any number of .htaccess files to the hosting account, but each directory or folder can only have one.

Meaning that it is able to add separate .htaccess files in the root directory and in its sub-directory, and in its sub-directory, and so on.

To Redirecting all URLs to a new website:

Redirect 301 / https://elayerforall.epizy.com/

This will redirect all URLs on the current(old) site to the new site.

To Redirecting a single URL page:

This will redirect visitors from a current(old) page to a new page without having to keep the old page.

Initially, if the file or page name was product.php and it's now changed to p.php.

Then use this line to redirect to p.php

Redirect /product.php /p.php

# if it is in root directory.

Redirect /path/to/old/file/product.php /path/to/new/file/p.php

# if it is in some other than the root directory.

To redirect to an enter new website or URL or external site:

Redirect /product.php https://elayerforall.epizy.com/index.html

# if it is in the root directory.

Redirect /path/to/old/file/product.php https://elayerforall.epizy.com/index.html

# if it is in some other than the root directory.

Other Redirections

RedirectMatch 301 ^/my/path/to /the/file/datails.php

Redirect /page.php /to/new/path/

Redirect /page.php /to-page.php

Redirect /path/to/other/site/ https://elayerforall.epizy.com/

There are other rewriting rules and configurations. We will discuss it again.

how to

About the Creator

Elayer For All

 Edu - Tech - Learn- Invest - Grow

Follow ElayerForAll on Vocal Media, Medium and Quora to read stories, lesson and articles.

https://medium.com/@elayerforall

https://elayerforall.quora.com/

https://www.instagram.com/elayerforall/

Enjoyed the story?
Support the Creator.

Subscribe for free to receive all their stories in your feed. You could also pledge your support or give them a one-off tip, letting them know you appreciate their work.

Subscribe For Free

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

    Elayer For AllWritten by Elayer For All

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.