.htaccess Redirect Rules & How to Implement Them


Websites are always changing. You add and remove content, change domains, restructure, and more. If you want your site to perform well amid all those changes, you need to know how to handle redirects. 

One way to do this at the server configuration level is to create redirect rules in an .htaccess file.

What Is an .htaccess File?

On Apache servers, .htaccess files enable you to make configuration changes, even if you don’t have access to the main server configuration files. 

.htaccess files operate on an individual directory basis. This can be useful if you need to create a rule that applies only to that specific directory and all of its subdirectories. This can also help you avoid editing your server’s main configuration files. 

.htaccess uses the same syntax as the root configuration, meaning any directives you would ordinarily add to the server configuration files can be used here.

However, Apache’s official documentation recommends that you only use .htaccess files when absolutely necessary, because they can sometimes slow down your server’s performance. So if it makes sense for your site, consider using other redirect methods, such as your server’s main configuration file, JavaScript, PHP, or HTML.

There are also available plugins, like Redirection on WordPress, that allow you to redirect pages without needing much knowledge on the topic.

Enabling .htaccess On Your Server

If .htaccess has not already been enabled on your server, you’ll need to enable it. You need to have sudo or root privileges to do this. If you don’t have root privileges, enlist the help of somebody who does.

First, open your website’s httpd configuration file. Then, after the VirtualHost() block, add the following: 

<Directory /var/www/test.com/public_html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted

Next, save and restart Apache. 

Some CMS systems like, WordPress or Drupal, automatically create .htaccess files. If you don’t have one, you can create it manually using a text editor. Save it as “.htaccess” with no other file extension, then add it to your site’s public_html folder in your root directory. 

Pro Tip: Before you make any changes to a configuration file (whether it’s your root configuration, or .htaccess) always save a backup. Mistakes can result in HTTP 500 Internal Server errors. With a recent backup file, if you make a mistake, you’ll be able to reverse your changes quickly.

Using the “Redirect” Directive in .htaccess

Using the redirect directive in .htaccess is fairly simple, once you understand the syntax. 

Below, we’ll break it down using a simple directive that redirects a single page to another on the same domain: 

First, highlighted in green, is the name of the directive. In this case, we’re using Redirect. Everything in .htaccess is case sensitive, so make sure you capitalize the R. 

Second, highlighted in blue, is the HTTP status code you want to use. This example uses 301 to indicate a permanent redirect. You can change this to whichever status code is most appropriate. If you do not include one, the system will automatically assume the redirect is temporary and use a 302 status code.

Third, in yellow, is the original location you’re redirecting visitors away from. The forward slash at the beginning of the location represents your entire current domain. Everything that follows tells you where the page you want to redirect is located. In this example, we’ve placed our page within a subfolder. 

If you were redirecting your entire domain, rather than an individual subfolder, you would only need to refer to it using a forward slash.

Finally, in purple, is the location of the target page, or the destination where you are sending your visitors. In this example, both the original and destination pages are located on your current domain, so the destination path begins with a forward slash. If you’re redirecting to a page on a separate domain, you will need to include the entire url. 

This directive can be adapted in many different ways, depending on what you need to redirect. The chart below includes 7 common redirects and how to write them.

Redirect Rule

How To Write It

Redirect between pages on the same domain: 

Redirect 301 /subfolder/old_page.html /subfolder/new_page.html

Redirect one page to another on a different domain:

Redirect 301 /subfolder/old_page.html https://www.exampleurl.com/subfolder/new_page.html

Redirect between subfolders on the same domain:

Redirect 301 /old_subfolder /new_subfolder

Redirect one subfolder to an external location:

Redirect 301 /subfolder https://www.exampleurl.com/

Redirect a domain to a subfolder on the same domain:

Redirect 302 /subfolder

Redirect between domains:

Redirect 301 /https://www.exampleurl.com

Redirect to a custom 404 page:

ErrorDocument 404 /error/pagenotfound.htm

Note: Custom 404 page redirects use a different directive name, but they are formatted the same way as the other redirect rules on this chart. If you’re using this, make sure you’ve already created your error page.

Redirect to HTTPS via the Rewrite Module

Some more complicated redirects use the Rewrite module (also referred to as mod_rewrite) rather than the Redirect directive explained above. The Rewrite module lets you manipulate and rewrite URL requests under specified conditions. 

For example, you’ll need to use this method if you want all URLs used to access your website to change automatically to HTTPS.

First, you need to enable RewriteEngine by declaring the following statement: 

RewriteEngine On

You only need to do this once per document, even if you’re creating more than one rule.

Once RewriteEngine has been enabled, you can use this code to rewrite all URL requests to use HTTPS:

RewriteCond %{https} off
RewriteRule ^(.*)$ https://www.ExampleURL.com/$1 [R=301,L]

The first line checks if HTTPS was already included in the current URL. In this statement, “off” means that the new rule will apply if the address does not contain HTTPS.

The second line creates the rule. When the condition is satisfied (i.e. when HTTPS is not included in the URL), the URL will be rewritten accordingly.

Finally, [R=301,L] means that this is an HTTP 301, or permanent redirect. The L means that this is the last rule in the set, and after running it, the server should return a result.

Check Your Redirects with Site Audit

Maintaining a site is complicated work that requires attention to detail, and it’s easy to make mistakes. Make sure your site stays healthy by auditing your site regularly. 

Site Audit can automate this for you, checking for errors, providing recommendations, and even helping you prioritize the most important fixes. It can check your redirects for issues like: 

  • Canonical links
  • Broken links
  • Temporary redirects
  • Permanent redirects

By creating effective redirects, and rigorously checking your work, you can create a site that performs well for your current users and in organic search.



Read original article here

Denial of responsibility! Search Engine Codex is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More