Skip to content

.htaccess SEO: How to improve SEO with .htaccess?

    In this .htaccess SEO series, we first discussed two methods to edit the .htaccess file—the proper, out-of-the-book method and a shortcut method. The second article was all about setting up .htaccess 301 and 302 redirects. This article is all about playing with the .htaccess file to improve SEO.

    The scope of .htaccess SEO is so broad—it can transfer all the website authority and links to an entirely new domain without losing traffic, make the website faster for returning visitors, redirect broken links as if nothing happened, and even enable compression for faster loading—all with a few lines of code. And this is why learning to optimize the .htaccess file is an important part of the game.

    How to optimize .htaccess for SEO

    Use 301 and 302 redirects for 404 pages

    As the website gets older and bigger, you might want to delete some of the older pages and files as they become obsolete. Some of the backlinks may point to a URL that no longer exists.

    Users and search engine crawlers will get a 404 error in these cases, which will negatively affect SEO. First, 404 pages are never good for SEO and user experience, and second, the backlinks will be of no use if they point to a 404 page. Considering how hard it is to earn a backlink, that’s a mistake nobody wants to make.

    You can use 301 and 302 redirects to redirect 404 pages to a live page on your website. If the redirection is permanent, use 301; if it is temporary, use 302 redirects.

    Here’s the code to permanently redirect example.com/oldpage to example.com/newpage:

    Redirect 301 /oldpage https://example.com/newpage

    Refer to our detailed article on .htaccess 301 and 302 redirects to set up perfect redirects on your website. Simply copy the code you’re looking for, replace the dummy URLs, and you’re good to go.

    Use a custom 404 error page to reduce bounce rate

    A generic 404 error page is not so good for both search engines and users. Since bounce rate plays a role in revenue and Google rankings, it is highly recommended to use a custom, engaging 404 error page.

    Once you have built a custom 404 page with elements that can keep users on your website, you can add the following code to the .htaccess file to use it as the default 404 page.

    ErrorDocument 404 /my-404-page

    Replace ‘/my-404-page’ with the URL slug of your 404 page.

    Enable compression to improve performance

    Website speed & performance are one of the top three SEO ranking factors today. Optimizing the core web vitals is critical for making the most out of the SEO efforts you make, and .htaccess can help with that.

    With only a few lines of code in the .htaccess file, you can enable file compression on the server side, reducing the size of files transferred and making your website load faster for users.

    Copy the following code and paste it into the .htaccess file just before the # End WordPress line.

    <IfModule mod_deflate.c>
    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
    </IfModule>

    Note that the above code uses DEFLATE, a lossless data compression algorithm similar to GZIP that works in Apache 2. Both algorithms are very similar in terms of performance, with DEFLATE being marginally better than GZIP, arguably.

    If you don’t want to compress certain file types, you can remove the associated lines of code. For example, if some CSS elements of your website are not working after enabling compression, you can try removing the line associated with CSS compression.

    Here’s the code to enable GZIP compression:

    <ifModule mod_gzip.c>
    mod_gzip_on Yes
    mod_gzip_dechunk Yes
    mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
    mod_gzip_item_include handler ^cgi-script$
    mod_gzip_item_include mime ^text/.*
    mod_gzip_item_include mime ^application/x-javascript.*
    mod_gzip_item_exclude mime ^image/.*
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
    </ifModule>

    Note that some web servers do not support GZIP, so if your website shows errors after enabling GZIP, you might want to try DEFLATE.

    Some websites may load faster without compression, especially if the server is slow. Make sure you test your website performance metrics before and after enabling compression.

    Enable caching to reduce load times

    Browser caching is an excellent method to reduce load times for returning visitors. When proper caching rules are set in the .htaccess file, it can reduce the load on the server, enhance user experience, and improve SEO.

    When a user visits your website for the first time, some of the static files are saved on their browser if caching is enabled. When the same user visits the website again, those files are loaded from their browser instead of your server, reducing the load on your server while loading the page faster for the user.

    Here’s the .htaccess code to enable browser caching:

    # BEGIN Expire headers  
    <IfModule mod_expires.c>  
      # Turn on the module.
      ExpiresActive on
      # Set the default expiry times.
      ExpiresDefault "access plus 3 days"
      ExpiresByType image/jpg "access plus 1 month"
      ExpiresByType image/svg+xml "access 1 month"
      ExpiresByType image/gif "access plus 1 month"
      ExpiresByType image/jpeg "access plus 1 month"
      ExpiresByType image/png "access plus 1 month"
      ExpiresByType text/css "access plus 1 month"
      ExpiresByType text/javascript "access plus 1 month"
      ExpiresByType application/javascript "access plus 1 month"
      ExpiresByType application/x-shockwave-flash "access plus 1 month"
      ExpiresByType image/ico "access plus 1 month"
      ExpiresByType image/x-icon "access plus 1 month"
      ExpiresByType text/html "access plus 600 seconds"
    </IfModule>  
    # END Expire headers 

    You can change the cache storage duration as per your requirements. For example, if your website is undergoing frequent JavaScript changes as part of development or testing, you can change the lines associated with JavaScript to reduce cache storage duration.

    Or, maybe just delete the line to disable caching for JavaScript.

    Block search engines from indexing specific files

    Google can often get deep into your website and index PDF files, Word documents, and other unwanted files. Users will directly access the file from Google instead of your website, losing valuable traffic for you. Also, some files are not to be indexed by Google.

    To prevent Google from indexing certain file formats, you can simply add the following code and paste it in the .htaccess file just before the #End WordPress line.

    <FilesMatch ".(docx|pdf)$">
        Header add X-robots-tag "noindex, noarchive, nosnippet"
    </FilesMatch>

    The above code tells Google to consider .docx and .pdf extensions as noindex, noarchive, and nosnippet. Replace the ‘docx|pdf’ with the file formats you want to block from Google.

    Optimize URLs for search engines

    Your website’s URL structure plays a not-so-silly role in SEO. The URL has to be short, yet it should have the necessary keywords to help search engines understand the content at first glance. It should also be free of unnecessary words, extensions, etc.

    .htaccess can help in removing unnecessary extensions at the end of the URL. For example, the following code removes the .html extension from the URL.

    RewriteEngine on 
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^(.*)$ $1.html [NC,L]

    To remove .php from the URL extension, use the following code.

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*)$ $1.php [NC,L]

    Congrats on optimizing your .htaccess file for SEO! Now you’ve got the foundation right, time to move to other aspects of technical SEO, all the best!