[.htaccess] Force removing www and index.php

One of the tweaks to my sites is trying to add consistency, as well as trying to remove duplicate content. The first step towards doing so is by considering if you want your domain to load with or without www, and with or without index.php.

In this article I am sharing the (mod_rewrite) .htaccess code that I use.

First, let’s upload an ASCII .htaccess file online by making a new file called htaccess.txt and renaming it to .htaccess

And start it with these defaults:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

If you want to remove the www. from the domain, so when someone types in www.example.com, it gets automatically rewritten to example.com, use this code:

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

And if you want to remove index.php from the url, so when someone types in example.com/index.php, it gets automatically rewritten to example.com, use this code:

RewriteCond %{THE_REQUEST} index\.php
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index\.php$ / [L,R=301]

I hope this helps you tweak your site, 🙂

Example URL:
https://www.mrfloris.com/index.php
should now end up as:
https://mrfloris.com


Posted

in

by

Tags: