Webstick.blog logo Wordpress, Design, SEO, Speed blog

What is an .htaccess file? [2025] 💥

What is an .htaccess file?


An .htaccess file redirects URLs. When using Wordpress you use a cache plugin which does a lot of the redirecting but sometimes manual work is needed to get thing just right.


Advertisement

Divi Ad 680px


Setting up an .htaccess file for an HTML site is easy if you just put the codes we give you here into your .htaccess file. If you're creating an HTML website, keep in mind that Google doesn't like duplicate content. In the first place, this relates to not stealing texts from other websites, but there is also a lot of duplicate content within your own website if you don't stop this.

This tutorial will give you the most important re-directs you should have in your .htaccess. P.S.: For a CMS website you can use many of these re-directs but in many cases working with plugins is recommended!

For the homepage alone, there are many examples of URLs that all display the same content, namely:

I use the URL of our website as an example..

  1. http://www.webstick.nl
  2. http://www.webstick.nl/
  3. http://www.webstick.nl/index.php
  4. http://webstick.nl
  5. http://webstick.nl/
  6. http://webstick.nl/index.php

All these versions also exist as https version!!


In the case of our website, all these URLs are redirected back to https://webstick.nl, which ensures that there is only one URL for Google to index. Redirecting URLs is called re-directing. We do the re-direction with an .htaccess file in the root of your domain, for Linux hosting this is the default folder public_html/ and on Windows hosting we don't work with .htaccess.

Windows hosting is not a pleasant hosting system, we do not work with this ourselves, you will not find information for this on this website, but there are plenty of other websites that can help you if you have this type of hosting.

An .htaccess is a plain text file that you rename on the server to .htaccess. I'm going to give you the re-direct codes for a Linux server:

To ensure that there is no trailing slash at the end of your URLs, put the following lines of code in your file:


#remove trailing slash

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301,L]

Remove .html at the end of URLs:


#remove .html

RewriteCond %{THE_REQUEST} \.html
RewriteRule ^(.*)\.html$ /$1 [R=301,L]

To redirect Http URLs to https when using an SSL certificate for a secure connection:


#http to https
    
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Compress files for faster loading time:


# compress text, html, javascript, css, xml:
	
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

# Or compress certain file types by extension:
<files *.html>
SetOutputFilter DEFLATE
</files>

File caching for faster loading time. Let browsers save and display files from memory. Use the entire code in 1x and you are immediately ready for all common types of files:


# Expires headers
<IfModule mod_expires.c>
 
ExpiresActive on
ExpiresDefault "access plus 1 month"
 
# CSS
ExpiresByType text/css "access plus 1 year"
 
# Data interchange
ExpiresByType application/json "access plus 1 week”
ExpiresByType application/xml "access plus 1 week”
ExpiresByType text/xml "access plus 1 week”
 
# Favicon (cannot be renamed!)
ExpiresByType image/x-icon "access plus 1 year”
 
# HTML components (HTCs)
ExpiresByType text/x-component "access plus 1 month"
 
# HTML
ExpiresByType text/html "access plus 0 seconds"
 
# JavaScript
ExpiresByType application/javascript "access plus 1 year"
   
# Manifest files
ExpiresByType application/x-web-app-manifest+json "access plus 1 week”
ExpiresByType text/cache-manifest "access plus 1 week”
 
# Media
ExpiresByType audio/ogg "access plus 1 year”
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType video/mp4 "access plus 1 year"
ExpiresByType video/ogg "access plus 1 year”
ExpiresByType video/webm "access plus 1 year”
 
# Web feeds
ExpiresByType application/atom+xml "access plus 1 day”
ExpiresByType application/rss+xml "access plus 1 day”
 
# Web fonts
ExpiresByType application/font-woff "access plus 1 month"
ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
ExpiresByType application/x-font-ttf "access plus 1 month"
ExpiresByType font/opentype "access plus 1 month"
ExpiresByType image/svg+xml "access plus 1 month"
 
</IfModule>
<filesMatch "\.(js)$"> 
ExpiresDefault A29030400 
</filesMatch>

To redirect Http URLs to https when using an SSL certificate for a secure connection:


#http to https
    
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Redirect all non-www urls to www:


#non-www to www
    
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Redirect all www urls to non-www. Replace our domain name with yours:


#www to non-www
    
RewriteCond %{HTTP_HOST} ^www\.webstick\.nl [NC]
RewriteRule ^(.*)$ http://webstick.nl/$1 [L,R=301]

Redirect the homepage URL with index at the end to a cleaner URL without index. Replace our domain name with yours:


#index redirect

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\\ HTTP/
RewriteRule ^index\$ http://webstick.nl/ [R=301,L]

Redirect all non-existent URLs to your 404 page. (Make sure you have created or are still creating a 404 page):


#non existing pages to 404 page  
                                    
ErrorDocument 404 /404

You can also redirect URLs that you have moved with an .htaccess file. Such a redirect is called a 301 re-direct. Replace our domain name with yours:


#single redirects 
                                    
Redirect 301 /website-laten-maken http://www.webstick.nl

301 re-directs issue a direct command to redirect from one URL to another, the first URL starts with a "/" followed by the page on the website, the second URL is mandatory and always starts with "http".

P.S.: lines starting with "#" are mnemonics for yourself, you can give a different description to this yourself.



Advertisement

Divi Ad 680px



Scroll up