Guide What is Htaccess?

Yoo

Member
Joined
Feb 8, 2024
Messages
21
Reaction score
40
Htaccess is short for Hypertext Access. It is an access configuration file used by Apache-based web servers.

Most of the cheap / shared web hosts out there have Apache-based web servers. A file called .htaccess when put into the main directory where your website sits - usually htdocs or public_html - grants you ability to configure redirection, evade website-access issues, do URL shortening and more. The 'dot' before the file name apparently makes it a hidden file in Unix-based systems. As a JavaScript programmer I use files that start with dots for files that are for the local environment or variable files for development.

You can use Htaccess to protect some pages with a password, create a custom 404 error page or most commonly, redirect a route to another page.

Simple Redirect Example with Redirect Directive:​

Redirect /old-page.html http://example.com/new-page.html

Redirect with RewriteRule Directive:​

Code:
RewriteEngine On
RewriteRule ^old-page\.html$ http://example.com/new-page.html [R=301,L]

This achieves the same result but gives you more control over conditions and the type of redirect (R=301 indicates a permanent redirect). See HTTP status codes for more. You already know what 404 is ;)

How to Redirect Without Changing the URL?​

If you want to internally rewrite URLs without changing what the user sees in their browser's address bar, you can use RewriteRule with the [P] flag (Proxy) or [L] flag (Last).

Code:
RewriteEngine On
RewriteRule ^internal-url$ /actual-page.html [L]

This internally redirects /internal-url to /actual-page.html without changing the URL in the browser.

Use .htaccess to redirect HTTP to HTTPS​

Code:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTPS} off checks if HTTPS is not already on.
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] redirects the request to https:// version of the same URL (%{HTTP_HOST}%{REQUEST_URI}), with a 301 status code (permanent redirect) and [L] ensures it's the last rule processed for this request. You can chain multiple rules in the same file. Dynamic variables are used for those routes.

Prevent Directory Listing​

Options -Indexes

This directive instructs Apache to disable directory browsing (listing the contents of a directory) for the directory and its subdirectories.

Other Uses of .htaccess:​

.htaccess files can do much more than just redirects:
  • Authentication and Authorization: Restrict access to certain files or directories.
  • Custom Error Pages: Show custom error pages for different HTTP status codes.
  • Cache Control: Control browser caching for your site's resources.
  • URL Rewriting: Change URLs dynamically, improving SEO or user experience.
  • Security Enhancements: Block specific IP addresses, prevent directory listing, etc.
  • Performance Optimization: Enable gzip compression, set caching rules, etc.
But the ones that I went into detail is all I ever needed .htaccess for :) before finally switching to my own root server with specialized Apache and Cloudflare configurations.
 
I miss this file since I switched Apache to Nginx. Nginx unfortunately doesn't work with .htaccess and in my opinion using nginx.conf is much harder than using .htaccess.
 

New threads

New posts

Back
Top