Common htaccess redirect rules

Everyone loves a redirect, they're the glue that holds a redesign together and the main reason your client's keep phoning you up. Here's a good chunk of them to use on your website.

By Luke Whitehouse on

PHPWordPress

Snippet

Sometimes it's a necessity, whether its a 301 temporary redirect or a more permanent rewrite rule for the entire website. Here's some of the more regular ones I've used in the past.

All of these rules should go within the .htaccess file of your website, if you do not have one then it should be located where your vhosts have been setup to, typically at the root of your project.

Site wide redirects

Redirecting your site to HTTPS

If you have an SSL certificate installed on your website then it's definitely advisable for any traffic coming in at http://yoursite.com to be redirected automatically to its https equivalent, for example: https://yoursite.com. This will be a deal-breaker for SEO.

<IfModule mod_rewrite.c>
  <IfModule mod_negotiation.c>
    Options -MultiViews
  </IfModule>

  RewriteEngine On

  RewriteCond %{HTTPS} !on
  RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

Redirect your site to www.yoursite.com

The www. subdomain has been a staple for websites for many years, and whilst there's no real benefit in using it anymore, some people prefer it as their users may expect it. Here's how you can drive traffic coming in at http://yoursite.com to http://www.yoursite.com.

Redirecting your site to HTTPs and www
<IfModule mod_rewrite.c>
  <IfModule mod_negotiation.c>
    Options -MultiViews
  </IfModule>

  RewriteEngine On

  RewriteCond %{HTTP_HOST} ^yoursite.com$ [NC]
  RewriteRule ^(.*)$ http://www.yoursite.com/$1 [R=301,L]
</IfModule>

Redirect your site to non-www.

On the flip side, if you'd prefer to remove the unnecessary subdomain but still cover your bases incase your users try to access it, then you can do that. This will drive traffic coming in at http://www.yoursite.com to http://yoursite.com.

Redirecting your site to HTTPs and www
<IfModule mod_rewrite.c>
  <IfModule mod_negotiation.c>
    Options -MultiViews
  </IfModule>

  RewriteEngine On

  RewriteCond %{HTTP_HOST} ^www.yoursite.com$ [NC]
  RewriteRule ^(.*)$ http://yoursite.com/$1 [R=301,L]
</IfModule>

Combining https and www redirect rules

If you wanted to combine the above rules, then you've got one of two options depending on whether or not you want www or not.

# https and non-www.
<IfModule mod_rewrite.c>
  <IfModule mod_negotiation.c>
    Options -MultiViews
  </IfModule>

  RewriteEngine On

  RewriteCond %{HTTP_HOST} ^www.yoursite.com$ [NC]
  RewriteRule ^(.*)$ http://yoursite.com/$1 [R=301,L]

  RewriteCond %{HTTPS} !on
  RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

# https and www.
<IfModule mod_rewrite.c>
  <IfModule mod_negotiation.c>
    Options -MultiViews
  </IfModule>

  RewriteEngine On

  RewriteCond %{HTTP_HOST} ^yoursite.com$ [NC]
  RewriteRule ^(.*)$ http://www.yoursite.com/$1 [R=301,L]

  RewriteCond %{HTTPS} !on
  RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

Content-specific redirects

Aside from site-wide redirects, here's some more specific redirects for pages within your website. These can come in handy if you change the URL of a page or even an entire section.

Redirecting one page to another

If you've moved a page on your website or you're looking to create an alias to a different page on your website, you can use a standard 301 redirect.

Redirect 301 /old-page https://yoursite.com/new-page

To give a little explanation on that rule, let's split it up into three sections. First of all Redirect 301 specifies that we are creating a temporary redirect on our website. Next, /old-page is the URI of the page we want to redirect from, relative to our website. This means that /old-page would be like your user going to http://yoursite.com/old-page. Finally, https://yoursite.com/new-page is the full URL of where the destination of the redirect resides. Notice that this is a full URL, meaning you could create a temporary redirect to an entirely different website.

Redirecting entire sections

When redirecting an entire section of a website (https://yoursite.com/old-section) to a new part of your website (https://yoursite.com/new-section) you might think that you could follow the same pattern as the Redirecting one page to another example above.

Redirect 301 /old-section https://yoursite.com/new-section

Whilst this would mean any user visiting https://yoursite.com/old-section would be taken to https://yoursite.com/new-section, it unfortunately doesn't take into account any pages within /old-section such as https://yoursite.com/old-section/the-force-is-strong. Therefore, we need to use a RedirectMatch rule instead.

RedirectMatch 301 /old-section/(.*) https://yoursite.com/new-section

This will ensure that any pages within /old-section will also be changed to /new-section. For example: https://yoursite.com/old-section/the-force-is-strong will become https://yoursite.com/new-section/the-force-is-strong.

Please bare in mind that within your .htaccess file RedirectMatch takes priority over a regular Redirect rule, meaning that if you wanted one specific page within a section to go to someplace else then you'd need to order your rules appropriately. Just like in CSS, the lower down your redirect rule the higher priority it has, so make sure any individual Redirect rules are above your RedirectMatch ones.

RedirectMatch 301 /old-section/(.*) https://yoursite.com/new-section
Redirect 301 /old-section/a-specific-page https://yoursite.com/different-page-altogether

Further reading

Until next time! ✌

Follow us on Twitter, Facebook or Github.
© Copyright 2021 Assortment.
Created and maintained by Luke Whitehouse.