Best .htaccess Guide for SEO – With Examples


hypertext access
hypertext access

After making a few changes to your .htaccess file, your website may have gone offline. But this isn’t a cause for concern! Many SEOs fall into this trap when they try to go above and beyond by doing technical fixes.

Normally, editing the .htaccess file through the WordPress dashboard causes a panic attack because you’re locked out of the dashboard once you’ve updated the incorrect .htaccess file. .htaccess files are both a blessing and a curse for SEOs because they allow you to change your website’s operations.

If your website runs on the Apache Web Server,.htaccess can help you with everything from password protection to 301 redirects and customised error pages.

This is the resource you thought didn’t exist if you’re seeking for a quick remedy to get your website back online after a.htaccess update failure or if you want to learn more about optimising your website’s.htaccess.

We’ll go through some quick.htaccess adjustments and advice so that you, as an SEO, may confidently make modifications to your website without fear of negative consequences.

You’re already aware of what.htaccess is and how useful it is for a webmaster using the Apache Web Server. However, for those who are new to.htaccess, I will go over the basics to assist them grasp it before implementing it.

What is .htaccess?

What is .htaccess
What is .htaccess

.htaccess is a configuration file that the Apache Web Server software can read and execute to enable or deactivate additional functionality and capabilities. Because the .htaccess file is created in a Unix-based environment and renders at the directory level, it overrides the global web server settings, allowing for specific website-access setup.

What does .htaccess stand for?

“Hypertext access” is abbreviated as .htaccess. After the file became popular among developers who used it to adjust user accessibility functions on a per-directory basis, the moniker was derived.

The Apache server’s http.config directives are used by .htaccess to enable and limit access to directories for users with usernames and passwords. When it comes to SEOs, however,.htaccess plays an even bigger role.

What is .htaccess used for?

Consider .htaccess a boon if you have a website that has a lot of different criteria on a regular basis.

As an SEO, you can use .htaccess to command the webserver to do 301 redirects, activate caching, alter HTTP headers, control crawling, and make URLs SEO friendly, among other things.

Where can you find the .htaccess file?

Because .Htaccess is a directory-level setting, you’ll find it in practically all of your web directory’s folders. The .htaccess file can be found in the root directory as well as inside each subfolder if you have a single web directory with several subdirectories (website).

If you’re using WordPress, the Yoast Plugin Option is the best way to get to the .htaccess file.

Caution: Because .htaccess is not something beginners should experiment with, I recommend doing it on the demo site first. If you get locked out of the WordPress Admin, you’ll need to know how to use Filezilla to get into the root folder.

There are a few plugins that allow you to alter the .htaccess file if you have a WordPress website. Because most WordPress users use Yoast as their default SEO plugin, I’ll show you how to change the .htaccess file from within the Yoast Dashboard.

Step 1: Go to your WordPress Admin Dashboard and log in.

Step 2: Go to the Yoast Settings page.

Step 3: Open the Tools menu.

Step 4: Select “File Editor” from the tools menu.

Step 5: Save the .htaccess file after editing it.

The default setting of .htaccess in most WordPress websites is as follows:

# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

What if your website doesn’t have a .htaccess file?

Because.htaccess is an automatically created configuration file, it will be present in 99 percent of circumstances. However, because it’s normally a hidden file within the directories, webmasters believe it’s gone missing. You should be able to find it if you enable “display hidden files.”

If you still can’t find the.htaccess file after all of this, you’ll have to manually create one and upload it to your web server.

Steps to create a custom .htaccess file

Step 1: Take a notepad and start writing.

Step 2: Write the configuration down (to test use the default configuration provided above)

Step 3: Save the file as.htaccess in ASCII format.

Step 4: Make sure the file isn’t saved in the.txt format.

Step 5: Upload the.htaccess file to your web directory with Filezilla.

Note: If you’re using WordPress and the.htaccess file field is empty, simply add the configuration and save.

What to do if you get locked out of WordPress Dashboard after updating the .htaccess file?

You may have been locked out of the dashboard after updating the.htaccess file with Yoast or WP File Manager. Don’t be concerned. This is a simple problem to solve.

If you’re an SEO, request FTP access to your website from the developer or server administrator. If you need assistance from a professional, your developer may be the ideal person to call.

Step 1: Open FileZilla and log in to your web directory.

Step 2: Open the directory and download the.htaccess file.

Step 3: Replace the default settings in the.htaccess file and save it.

Step 4: In the directory, replace the old.htaccess file with the new one.

Use the default code if your priority is to get the website back online as soon as possible. If you’ve made a lot of changes to the.htaccess file, look for the code that went wrong. Remove it from the file before re-uploading it to the server.

Why are you seeing an error after updating the .htaccess file?

If you’re an SEO trying to learn .htaccess from the ground up, you’re bound to make a few blunders at first. This is OK until you figure out why the web server reported an issue.

When it comes to changing the .htaccess file, there are a few frequent issues that SEOs face. The most common .htaccess issues are listed below.

  1. Disabled Override: You must first enable AllowOverride for your.htaccess file to work. All customizations set in the.htaccess will be disabled if the AllowOverride option is set to None. To make sure the Override option is turned on, do the following:

Step 1: Navigate to the Apache Configuration File and double-click it (http.conf)

Step 2: Change Allow OverRide to AllowOverride All in the AllowOverride directive.

Step 3: Save the Apache Configuration file and restart Apache.

  1. Misspelled File Name: The second most common SEO error is misspelling the file name. Misspelling.htaccess will result in an error because it is a Unix-based configuration file saved in ASCII. The Apache server will ignore the file and the configuration you provide if the file does not begin with a “.” or is uploaded in a different file type, such as.txt.
  1. The Hierarchy of.htaccess Files Is Important: The Hierarchy of.htaccess Files Is Important: The Hierarchy of.htaccess Files Is Important: The Hierarchy of.htaccess Files I A few rules added to the beginning of the.htaccess files may override rules added later in the configuration. If you believe these are crucial, consider moving the configuration to the top.
  1. Multiple.htaccess files: Because.htaccess files can be used per-directory, your website could be using multiple.htaccess files. In such circumstances, one file’s configuration may conflict with another’s, resulting in problems. Individual.htaccess files can be disabled to troubleshoot this.
  1. Syntax Error: The.htaccess file’s functionality is entirely dependent on the syntax used to set up your website. A file syntax problem can take your site offline, causing a panic attack. As a result, it’s critical to understand the syntax before making changes to your.htaccess files.

What are the common error messages that you get after updating the .htaccess file?

A user interacts with the webserver directly or indirectly every time he or she views your website. The web server fetches the sites they see when users click on the photos and other resources they see, therefore you can limit it as the webmaster.

.htaccess is a file that some websites use to set authentication for accessing their pages. When it comes to SEOs, they employ it to make sure that visitors and search engine bots have quick access to the pages that are most important to them. The web server generates an error code based on the configurations defined in the .htaccess file if the website fails to supply the desired information.

Here is the list of error codes that web servers display once they fail to fetch the requested data.

Client Request Errors

  • 400 — Bad Request: Invalid URL Structure. The server is unable to understand the request raised by the user. 
  • 401 — Authorization Required: These messages get displayed when the access to the page has been restricted by the webmasters. 
  • 402 — Payment Required (not used yet): If the payment initiation fails to happen, it generally gives this code.
  • 403 — Forbidden – The simple reason you get a 403 error is because you’re trying to access a resource that has restricted permission. A website displays 403 forbidden errors when the users try to access a page that requires authentication. 
  • 404 — Not Found: 404 is a clear indication to the users that the requested URL is not available on the website. It can be because of a typo error in the URL or when the page has been removed from the site.
  • 405 — Method Not Allowed: This HTTP response status code indicates that the server has refused to accept the request method despite understanding the purpose of the request. 
  • 406 — Not Acceptable (encoding): This usually happens when the server cannot respond with the accept-header request.
  • 407 — Proxy Authentication Required: This error indicates that the request cannot be fulfilled because of the lack of proxy server authentication between the browser and the server. 
  • 408 — Request Timed Out: This is one of the common HTTP errors that webmasters encounter when the server fails to receive a complete request from the client side within the allotted timeout period.
  • 409 — Conflicting Request: This error happens when the state of the target resource conflicts with the current state. To resolve the error, identify the conflict and resubmit. 
  • 410 — Gone: This error code represents that the access to the requested resource has been permanently removed from the server and it will remain so throughout.
  • 411 — Content Length Required: The error represents the inability of the server to accept the client request due to the failure to define the content-length header.
  • 412 — Precondition Failed: The is an error caused due to the security clash with one or many of the security configurations that have been implemented on your server. 
  • 413 — Request Entity Too Long: When the requested resource is too large for the server to load, the user may experience the 413 error. 
  • 414 — Request URI Too Long: Just think of a URL structure above 2048 characters. The server cannot decipher the resulting 414 error. 
  • 415 — Unsupported Media Type:  This error shows up when the server refuses to load a resource that is in an unsupported media format. 

Server Errors

  • 500 — Internal Server Error
  • 501 — Not Implemented
  • 502 — Bad Gateway
  • 503 — Service Unavailable
  • 504 — Gateway Timeout
  • 505 — HTTP Version Not Supported.

What is .htaccess used for?

What is .htaccess used for
What is .htaccess used for

Redirects

Have you recently changed your website’s domain name?

As an SEO, you don’t want your users to see a 404 page, and you certainly don’t want all of your hard-earned authority to evaporate.

Consider your .htaccess file as a solution to both of these issues. You can redirect your traffic and site authority to the new domain by adding the redirect directive to your .htaccess file.

The interesting part is that you can do the same thing with your website’s URLs. You can force users and search engine bots who are trying to access an old URL to view a new page on your site by using a 301 redirect directive.

With .htaccess, here’s an example of a domain-level redirect:

# This allows you to redirect your entire website to any other domain 
Redirect 301 / http://example.com/Opens in a new tab.

Using .htaccess, here’s an example of a URL redirect:

RedirectMatch 301 ^/old-url.html$ /new-url.html

SEO-friendly URLs

Is the structure of your URLs messed up? Are visitors and search engine bots having trouble deciphering what’s on the page? This is something that a lot of webmasters face.

After a website obtains authority, failing to pay attention to the URL structure becomes a major issue.

You may set the right URL format for your website using .htacces.

Furthermore, any extension that comes with your URL, such as.html or.php, can be simply eliminated by adding directives to the .htaccess file.

Example 1: Using .htaccess to remove extensions in the URL

Removing .php as Extension

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

Removing .html as Extension

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

Example 2: Using .htaccess to make URLs lowercase

Method 1

RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule . ${lc:%{REQUEST_URI}} [R=301,L]

All URL structures, including the domain name, will be affected by the aforementioned code.

Method 2

RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^.+.html$ ${lc:%{REQUEST_URI}} [NC,R=301,L]

Only the html file names will be affected by the preceding one.

Example 2: Using .htaccess for Dynamic URL rewriting

RewriteEngine On
RewriteRule /(.*)/(.*)/$ page.php?category=$1&product=$2

Bad URL: site.com/page.php?category=2&product=54

Good URL:  site.com/sandwiches/rueben-sandwich/

  • Using .htaccess to Improve Site Speed

The new buzzword among webmasters is page speed.

They have every reason to cause a ruckus, as Google now considers it to be one of the most important elements in ranking pages on the SERP.

Google does not want users to have a negative experience when visiting the top-ranked pages.

A slow-loading website also wastes a lot of the crawl budget.

Configuring your .htaccess file is one of the simplest and safest ways to boost the speed of your website.

  • Enabling Cache Feature

By enabling the cache feature in the .htaccess file, the website’s resources are saved on the visitor’s browser, allowing for faster loading.

You can enable cache using two different methods

ExperiesByType — You can specify a cache time window, aka expiration, for each resource on your website using this method in.htaccess.

Example:

<ifModule mod_headers.c>
# YEAR
<FilesMatch ".(ico|gif|jpg|jpeg|png|flv|pdf)$">
Header set Cache-Control "max-age=29030400"
</FilesMatch>
# WEEK
<FilesMatch ".(js|css|swf)$">
Header set Cache-Control "max-age=604800"
</FilesMatch>
# 45 MIN
<FilesMatch ".(html|htm|txt)$">
Header set Cache-Control "max-age=2700"
</FilesMatch>
</ifModule>

Cache-Control Header — The cache-control header uses the resource’s maximum age before expiration.

Example:

# One month for most static assets
<filesMatch ".(css|jpg|jpeg|png|gif|js|ico)$">
Header set Cache-Control "max-age=2628000, public"
</filesMatch>

Enabling Gzip Compression

The size of a website’s resources is the single factor that slows it down. You can lower the picture size, file size, and file number while transmitting data to the client side browser by activating gzip in your .htaccess file.

One of the simplest .htaccess directives is to enable gzip compression.

Here’s an illustration:

<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>

Enabling Deflate Option

Because some web servers do not support gzip, the website may encounter difficulties. In such instances, the deflate option in the .htaccess file is recommended.

Here’s an illustration:

<IfModule mod_deflate.c>
# Compress text, HTML, JavaScript, CSS, XML
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
# The following lines are to avoid bugs with some browsers
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/html
BrowserMatch bMSI[E] !no-gzip !gzip-only-text/html
# Do not cache if these files are already cached
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip
# Proxies must give the right content
# Header append Vary User-Agent env=!dont-vary
Header append Vary User-Agent
</IfModule>

Using .htaccess for Improved Crawling and Indexing

You may already be utilising the robot.txt file to control whether or not search engines can crawl and index your site. However, it’s possible that your site contains resources other than a webpage. The rbot.txt may not work in such instances.

Setting up an X-robots-tag in your .htaccess file is the ideal technique to make a few resources, such as a PFD or a Word Document, no-indexed.

The .htaccess file’s custom header is compatible with all indexer directives.

Example:

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

Any file with the extensions.doc and.pdf will be regarded noindex, noarchive, and nosnippet in this situation.

Almost every Apache server comes with a pre-configured configuration file. However, because this applies to the entire site, setting directory-level customisation is difficult.

This is where .htaccess comes in handy. The .htaccess file can be used to override Apache configuration settings at the directory and subfolder level.

In addition, you can set authentication using simple setup codes. This is especially beneficial if you have many websites on a shared hosting account.

Advantages of .htaccess file

  • All requests are read by it.
  • Without having to restart the server, a modification can be made right away.
  • Manage user access effectively based on their preferences
  • Sets the configuration at the directory level.
  • For SEOs, this is a great boon.

Disadvantages of .htaccess file

  • The use of.htaccess files can enhance the security concerns associated with hosting a website.
  • Because the.htaccess file is scanned and read every time a page loads, it is slower than server-side setup.
  • It has an effect on the speed of a website that receives millions of visitors.

Due to security and performance concerns, .htaccess files are not advised as a means of server setup.

Recent Posts