Quickly build your .htaccess file

Setting up 301 redirects is a common SEO activity to fix broken link problems or to do a site move. This online tool helps you create those redirects on the popular Apache server used by most hosting providers. CMS systems like WordPress, Joomla and Magento are normally on this type of server.

An .htaccess file is a file on Apache servers used for several things including controlling how different URLs are processed. One common use is when a website is re-written and you want to forward all the users visiting your old pages to the new ones.

In the .htaccess file, this is done by adding RewriteRules that cause 301 Redirects from one page to another. The 301 Permanent Redirect is the best way to perform forwarding as it also tells the Search Engines to update their indexes.

Manually creating these RewriteRules can be time consuming and error prone. This tool will generate the rules for you.

Not using Apache?
Then check out my article on how to do 301 redirects, which covers examples for other servers and languages.

Enter a comma or tab separated lists of old to new pages then click the generate button. Copy the resulting code into your .htaccess file then see if the pages are redirecting as expected.

I find the simplest way to do this is to create an Excel file with two columns. Place the old pages in the first column and the new pages in the second column. Then just copy and paste the cells into the form below.

You only need to enter the path for each page and not the whole URL. e.g. about-us.htm. Just using paths means the rules generated are domain name independent. If you enter the full URL then the code generated will also take the domain into account. This way you can have the rule only apply to a single domain or have the redirect switch to a different domain.

Want to test your Redirects?
Then use my Redirect Header Checker Tool to see what's happening.

An example list may look like this:

index.php?page=45, contact-us/
about-us.htm, about-us/
product-details.php?id=345, product/345/?view=details

The .htaccess 301 Permanent Redirect Generator

Domain Redirects
Protocol Redirects
Old to New Page List
Allow and pass on extra query parameters
Custom Field Separator (Tab or Comma by default)

What is the .Htaccess Redirect Generator doing?

Here's an overview on what it does

RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]

For each rule there may be conditions that must be met before a rewrite rule is considered. These Rewrite Conditions are added using the RewriteCond command. The above one is used if the old path included a specific domain (HTTP_HOST) to use.

RewriteCond %{QUERY_STRING} ^querystringtext$

Dynamic URLs contain querystring parameters after a question mark (?). These have to be specified as a rewrite condition (QUERY_STRING). If no querystring is used then a blank rewrite condition is added to make sure it only matches URLs without query strings.The condition is written using Regular Expression (Regex) syntax.

RewriteCond %{HTTPS} =on
Some Regex Basics
^The start of a string
$The end of a string
\.Dots need escaping with a slash as they have a meaning (any character)
\+Plus needs escaping with a slash as it means "one or more" of the previous character

The above rewrite condition is added if the old path specifically used https:// in its URL, thus stating it's secure.

RewriteRule ^about-us\.htm$ /about-us/? [R=301,NE,NC,L]

Then the actual RewriteRule. This first states the path that must be matched for the rewrite rule to be used (once all the previous rewrite conditions are met). This is in a Regular Expression (Regex) syntax.

The generator creates a Regex string that enforces an exact match with the supplied path.

Next we add the page we are Permanently Redirecting to. This may be a relative path or a complete URL, depending on the format used in the original list. If the URL does not contain a question mark (?) then one is added. This is a signal to stop the querystring from the old page being added.

Finally a set of flags are included to define how the rewrite rule works. They specify it should be a 301 Permanent Redirect (R=301), the URL should not be escaped (NE), it should not be case sensitive (NC) and it is the last rule that needs to be processed (L).

Why don't I use the shorter Redirect command

There is a lot simpler Redirect command that can do redirects in one line. e.g.

Redirect 301 /testsource.html /testdestination.html?a=b

The main reason I don't use it is because my tool is designed to do exact URL redirecting. The Redirect command does not consider incoming query strings which means an exact source match can't be done. e.g. the above command will also match /testsource.html?parameter=value

Another limitation is that you can't restrict the source to a specific domain. However you can specify the domain of the destination, and specify a query string (any source query string is always ignored and lost).

What if I have lots of 301 redirects!

The bigger the .htaccess file gets the slower it will become. At some point this may have an adverse effect on site speed and user experience.

One solution is to switch to a database driven redirect system. Databases offer fast lookup of large datasets so scale a lot better than the .htaccess file solution. Some CMSs provide plugins for this or you could get your developer to build one. For WordPress I recommend Redirection. CMS based solutions can often make it a lot easier to manage those redirects.

Another option is to use Apache RewriteMap. It is quite technical but it allows you to create lookup tables and therefore will scale better than the basic RewriteRule commends. The maps need to be added at the server level which means this is not an option for those using shared hosting or with limited control of their servers.

Can I help you improve the tool?

If you spot any issues or have some ideas on how I can improve it, make a comment below or directly contact me . I'd love to hear from you.