For customized website performance and function .htaccess files allow developers to override or enable the main server configurations on web server software. By replacing the main server’s configuration files with your own, the .htaccess file is loaded to the web server. It’s then detected and executed by the server to alter the configuration of the software for custom functionality and features that the software typically doesn’t offer. By configuring certain server files, SEO performance is increased as you‘re able to customize areas the server may not offer.
.htaccess Files for SEO
- How .htaccess Files Help SEO
- SEO-Friendly URLs .htaccess
- Overriding CMS URLs
- Removing .html and .php
- Adding rel=“canonical” to PDFs and Headers
- Utilizing .htaccess for improve site speed
- Developing Various Redirects
- Redirecting to the Latest Website Version’
- Solving 404 Errors with 301 Redirects
- Caching for Site Speed
- Robot Directives
- Sending Spiders to Sitemap
- Configuring .htaccess Files
How Do .htaccess Files Help SEO?
Configuring the main server’s files will directly result in better SEO because you’ll be spotted by more search engine spiders and increase how well your website functions. By replacing the standard files with your own, you’ll produce cleaner URLs, resolve missing 404 status codes, produce better 301 redirects, and boost the cache of your website. There are many useful codes every web developer should know when dealing with .htaccess files.
Creating SEO-Friendly URLs .htaccess
You need to have clear and concise SEO-friendly URLs that include keywords to draw more traffic from search engines as Matt Cutts verifies that keywords in your URL are used as a ranking factor. Not to mention, URLs need to be as short and to the point as possible for internet users to remember them easily and understand where a link is taking them.
Clean up the appearance and function of your URLs by overriding the current URLS on a file-by-file basis as extensions are used to identify and handle URLs.
Working with https://site.com/index.php?topic=article as an example, use htaccess code:
RewriteEngine On
Rewrite Rule ^topic/ (a[a-zA-Z0-9]+)$ index.php?topic=$1
Thus, changing the previous URL to https://site.com/rules/article.
Overriding CMS URLs
There are a few instances where a CMS won’t allow for easy customization of URLs. You’re able to find your around the CMS’s restrictions with .htaccess files using:
RewriteEngine On
Rewrite Rule (.*)/$load_page.php?&page_id=$1
Upon uploading the new file, the URL will appear much cleaner and perform better for SEO, appearing as: site.com/contact-us
Removing .html and .php
Although Matt Cutts recommends keeping file extensions, such as .html, many don’t like the appearance and want the extension removed. To remove .html and .php extensions use .htaccess code:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
Rewrite Rule ^(.*)$ $1.html
To remove .php, you’ll just swap .html with .php in the above code. Be sure to remove the extension from all of your internal links as well.
Adding rel=“canonical” to PDFs and Headers
In 2011, Google announced their support of adding rel=“canonical” relationships with specific HTTP headers to signal canonical URLs for HTML documents, including PDF files. When placed in the head section of an HTML page, it’s an alternative method to point images and PDFs to HTML versions, such as a page where downloading a PDF document is available.
For PDF files using HTTP headers, you’ll use the code below to point the PDF file to the HTML page using the /page.html URL.
<Files “file.PDF”>
Header and Link “<htttp://www.site.com/page.html>; rel=”canonical””
</Files>
Utilizing .htaccess for improve site speed
This can be done by leveraging browser catching in a few ways:
- Using a Cache-Control header
- Compressing your site
- Utilizing an ExpiresByType
Developing Various Redirects
Configuring .htaccess files will resolve multiple issues with redirects, such as redirecting visitors to the correct version of your website, 404 redirects, and 301 redirects. You never want a website visitor to end up on a blank page due to a broken URL, which is why you need to establish great redirects that abide by the latest algorithm updates.
Whether you need to create a custom 400 for a bad request, 401 for authorization required, 403 for forbidden directory, or 500 for internal server error, the code is simple to send visitors to the right path using:
ErrorDocument 402 /temp/page-temporarily-unavailable402
For 400, 403, and 500 redirects, simply change the number in the above code to properly redirect.
Redirecting to the Latest Website Version
For whatever reason, a search engine may be indexing multiple versions of your homepage. To sort the confusion for search engines and to send website visitors to the most recent version of your website, you need to tell search engine spiders where to go with a 301 redirect.
To change the www. version with a 301 redirect use .htaccess code:
RewriteCond %{HTTP_Host} ^site.com$ [NC]
RewriteCond ^(.*)$ https://www.site.com/$1[R+301,L]
RewriteCond %{THE_REQUEST} ^.*/index
Rewriter ^(.*)index$ https://www.site.com/$1 [R=301, L]
If you prefer a naked URL without the www., use the above code but leave out www.
Solving 404 Errors with 301 Redirects
We all know 404s can’t always be avoided, but you can resolve a 404 error with a 301 redirect. As a result, no one will ever see the 404 error because you’ll bring older website pages to newer website pages. This will work for all search engines, not just Google, to allow all of your pages to be indexed. It will also solve the issue of any website that may have linked to your older pages, so there won’t be any broken links as they will be redirected.
Redirect your older URLs to newer URLs using:
Redirect 301/information/your-article https://www.site.com/articles/your-article
Caching for Site Speed
You will improve your website’s speed by adjusting caching. Caching involves a browser storing certain resources, like images and text files, which are triggered when a visitor heads to your website. You’re able to adjust how the browser reacts when an internet user visits your website using mod_expires and mod_headers, providing you with greater control over the caching process.
To adjust the expiring time, use .htaccess code:
<ifModule mod_headers.c>
# Turn on Expires and set default expires to 2 days
ExpiresActive In
ExpiresDefault A259200
Adjust the expiring times to meet your own needs; however, remember they are calculated in seconds.
Robot Directives
You may or may not want Google to access all website pages for indexing. Restricting robots.txt and “no follow” links aren’t always an option with some CMS. If the options aren’t available, you’ll need to use .htaccess to put the “No Index Meta Robots” tags on the pages you want restricted.
For example, if you don’t want search engines to show PHP files you’ve generated, insert the following code into the header file:
Header(“X-Robots-Tag: no index”, true);
To make the links on the page “no follow,” use code:
Header(“X-Robots-Tag: no index, no follow”, true);
To directly configure the web server, use:
<Files Match “robots.text”>
Header set X-Robots-Tag “no index”
</FilesMatch>
Don’t worry about making your links “no follow” as Google says they won’t penalize for “no follow” links.
Sending Spiders to Sitemap
For a well ranking site on Google, search engine spiders need to be able to crawl the sitemap to index pages, posts, categories, and tags. If your website doesn’t allow for automatic regenerations of new sitemaps when your website is updated, add the appropriate .htaccess code. By adding a HTTP 301 Redirect, you’ll create a permanent redirect to tell search engine spiders exactly where the file is located to increase your ranking.
< IfModule mod_alias.c >
RedirectMatch 301 /sitemap.xml$ https://site.com/sitemap.xml
RedirectMatch 301 /sitemap.xml.gz$ https://site.com/sitemap.xml.gz
Configuring .htaccess Files
There are many advantages and disadvantages to editing .htaccess files. There are great benefits because the changes will be immediate to boost the functions of your site. But, if you don’t know what you’re doing, you’ll hurt the performance of your website and cause some serious damage. Not to mention, if it isn’t done correctly, it may cause security issues. If you aren’t sure what you’re doing, don’t touch it! It’s best to leave the server alone and hire a professional to configure .htaccess files correctly.
FAQs
1. What is an .htaccess file in SEO?
A .htaccess file is used to redirect, adjust HTTP headers, crawl sites, and other vital SEO functions.
2. How do I create an .htaccess file?
Creating a .htaccess file is straightforward, create the file with a text editor such as Notepad, Microsoft Word, and more.
Name the “.htaccess”, add the text you want, and upload the file using FTP client.
Once you have FTP connection move the folder to the hosting space you wish to have it in.
3. Why can’t I see my .htaccess file?
By definition .htaccess is a hidden file so it will not be viewable by accessing the web server.
4. How many .htaccess files for SEO should I have?
You should have one .htaccess file for your entire web hosting account.
Sources:
“Configuring .htaccess” Oregon State University