A fresh mix of code snippets and tutorials

Remove www and index.php from URL

Here’s a handy .htaccess snippet for canonicalizing URLs by removing the “www”, “index.php”, and “index.html” from all requests.

To make it happen, backup your current .htaccess file and then add these rules:

<IfModule mod_rewrite.c>
 # remove index
 RewriteCond %{REQUEST_URI} !/$ [NC]
 RewriteCond %{REQUEST_URI} /index.(html?|php)$ [NC]
 RewriteRule .* http://example.com/ [R=301,L]
 # remove www
 RewriteCond %{HTTP_HOST} ^www\.example.com$ [NC]
 RewriteRule .* http://example.com/$1 [R=301,L]

Then edit each instance of example.com with your own domain and upload to your server. To verify that it’s working, try requesting some pages while including the “www”, “index.php”, and “index.html” in the URLs.

Learn more

.htaccess made easy