Image default
WordPress Tutorials

How To Create & Edit .htaccess File using Rewriterule

What is htaccess file

.htaccess is a hidden file that is used for your website’s configuration. Using it, you can protect password directories, do URL rewriting, disallow access to specific IP addresses, enable hotlink protection, change your website’s time zone or default index page and much more. This tutorial shows how to create .htaccess file by using File Manager in your hosting control panel.

Steps to Follow

Before you read this guide you’ll need the following:

Where is htaccess

Access to your hosting account’s control panel

Step 1 — Locating and Opening File Manager

Go to your hosting account’s control panel and open up the File Manager tool located in the Files category:

File Manager Htaccess

After clicking on the file manager you go to settings and check the show hidden file option.

Domain Preferences

Step 2 — Locating .htaccess file in File Manager

.htaccess file is located in your public_html directory. You can easily access the file and it’s content by clicking and selecting Edit:

.htaccess edit

If your hosting platform uses cPanel, then the process is easy.

edit Htaccess

Step 3 — Creating .htaccess file if it is not present

If there is no .htaccess file then create a new .htaccess file

create htaccess

After that, write the name .htaccess and press Create to save it. You will then be able to access your newly created file and put the code.

Redirect to www subdomain

This snippet redirects all non-www requests to the www subdomain

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

Redirect to HTTPS

This snippet redirects all non-HTTPS traffic to HTTPS

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

PHP file to handle all non-static requests

In PHP it allows you to read the actual requested path in the $_SERVER[‘REQUEST_URI’] global variable.

 RewriteEngine On
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule ^(.*)$ index.php [QSA,L]

Rewrite GET parameter to URL part

If you have an URL that you want to call with “GET /orders?id=13” and you want it to respond as if “GET /orders/13” was called, then you may use the following snippet code

 RewriteEngine On
 RewriteCond %{REQUEST_URI} ^/orders [NC]
 RewriteCond %{QUERY_STRING} ^id=([0-9]+)$ [NC]
 RewriteRule ^(.*)$ %{REQUEST_URI}/%1\? [R,L]

Related posts

7 Excellent Website Ideas for an Online Side Business


15 Ways to Speed Up WordPress Performance in 2019


How to Protect Your WordPress Site from Rest API Attacks