How to use PHP htaccess file

Just create a file with .htaccess file in your website folder.

For Redirect website http://example.com to http://www.example.com

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


For Redirect website http://example.com to https://www.example.com

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

When you have to redirect the website non HTTP to HTTPS protocol after installing an SSL certificate on your domain.


For the Redirect 404 page

RewriteEngine On
ErrorDocument 404 https://www.example.com/404.php

While the browser is not found any page on the mentioned URL you want to found.


Create SEO Friendly URL or User-Friendly URL using Htaccess PHP

Like http://www.example.com/page-name.php http://www.example.com/page-name

RewriteEngine On
RewriteRule ^page-name page-name.php [NC,L]

Like http://www.example.com/user.php?id=something to http://www.example.com/user/something

RewriteEngine On
RewriteRule ^user/(.*)$ user.php?id=$1 [QSA,L]

Like http://www.example.com/ category.php?catid=something&title=$2 to http://www.example.com/ category/something/something2

RewriteEngine On
RewriteRule ^category/([0-9a-zA-Z]+)/([0-9a-zA-Z]+) category.php?catid=$1&title=$2 [NC,L]

Leave a Reply