...
Royalhost Blog
Web Hosting

What is htaccess? Everything about .htaccess file

The .htaccess file, known as “hypertext access,” is a configuration file used on web servers. It allows you to modify the server’s behavior for specific directories or files, overriding the default settings defined in the main server configuration.
The .htaccess file is plain text in a website’s directory (Public_html). It can control various aspects of website functionality, such as URL rewriting, access control, redirections, MIME types, error handling, and more.

Some common use cases for .htaccess include:

URL rewriting: You can use mod_rewrite module directives to rewrite URLs and create search engine-friendly URLs or redirect requests to different files or directories.
Access control: You can restrict access to specific directories or files based on IP addresses, user agents, or authentication requirements.
Redirection: You can redirect requests from one URL to another temporarily or permanently.
MIME types: You can define custom MIME types for specific file extensions.
Error handling: You can customize the error pages displayed to users when encountering specific HTTP errors.

Some other uses of the .htaccess file:

Add redirections for specific URLs
Load custom error pages, like 404 pages
Force your site to use HTTPS instead of HTTP
Password-protect specific directories on your server
Prevent hotlinking

It’s important to note that not all web servers support .htaccess files. They are specific to the Apache HTTP Server and its variants like Apache Tomcat. Other web servers, such as Nginx, have their methods of achieving similar functionality.
When working with .htaccess files, it’s crucial to be cautious, as incorrect configuration can cause issues with your website. It’s recommended to have a backup of the original file and test any changes thoroughly.

.htaccess File Location

Where is the .htaccess file located?
.htaccess file is located in the Public_html folder.
Apache (the software that powers your web server) or some other web server is configured to hide all files named .htaccess. Why? The files have essential configuration information and can be used to compromise your server.
Although the file is hidden, the .htaccess file location is most commonly found in your website’s public_html folder.
.htaccess Location
You can access your .htaccess file in a few different ways:
From your hosting account’s file management (such as via cPanel)
From an sFTP or FTP client
Tip: If you don’t see your .htaccess file in your public_html folder, you may have to check “Show Hidden Files.”

How to edit a .htaccess file?

You can use your host control panel file editor to edit this file or use FTP Programs.

Some common codes for .htaccess file:

Forcing HTTPS code with .htaccess

#force SSL on entire site
RewriteEngine On 
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://mysite.com/$1 [R=301,L]

Hotlink Protection code with .htaccess

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)mydomain.com/.*$ [NC]
RewriteRule .

Custom 404 Error Page code with .htaccess

ErrorDocument 400 /errors/400.html

Denying specific IP with .htaccess

Order Deny,Allow
Deny from 192.168.1.1

Denying list of IPs with .htaccess

Order Deny,Allow
Deny from 192.168.1.1
Deny from 192.168.1.2

302 (Temporary) Redirect with .htaccess

Point an entire site to a different temporary URL. This is useful for SEO purposes when you have a temporary landing page and plan to switch back to your main landing page at a later date:

(This allows you to redirect your entire website to any other domain)

Redirect 302 / http://example.com/

Redirect index.html to a specific subfolder:

Redirect index.html to a specific subfolder with .htaccess

Redirect /index.html http://example.com/newdirectory/

Redirect an old directory to a new directory with .htaccess

Redirects example.com/old to example.com/new

RewriteRule ^old/(.*)$ /new/$1 [R=301,NC,L]

Redirect old file path to new file path with .htaccess

Redirect /olddirectory/oldfile.html http://example.com/newdirectory/newfile.html

Show the content in example.com/folder2, but the URL appears as example.com/folder1

RewriteEngine On
RewriteRule ^folder1/?$ /folder2/

To show the URL as just example.com with .htaccess

RewriteEngine On
RewriteRule ^/?$ /folder2/

WordPress default .htaccess file

# BEGIN WordPress

RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

Related posts

Crypto Web Hosting, Buy hosting with cryptocurrencies

admin

Perfect Money Hosting, Buy Hosting and Domain with Perfect Money

admin

What is Bitcoin Hosting? Everything about Bitcoin Web Hosting

admin

Leave a Comment