.HtAccess 301 Redirect Code Generator Tool
Quickly build your .htaccess file
Setting up 301 redirects is a standard SEO activity to fix broken links or complete 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 usually are on this server type.
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 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.html. Just using paths means the rules generated are domain name independent. If you enter the full URL, 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.
An example list may look like this:
index.php?page=45, contact-us/
about-us.html, about-us/
product-details.php?id=345, product/345/?view=details
What is the .Htaccess Redirect Generator doing?
Apache servers use the .htaccess file for several things, including controlling how different URLs are processed. One frequent use is when a website is re-designed, and you want to forward all the users visiting your old pages to the new ones.
This is done by adding RewriteRules to the .htaccess file that causes 301 Redirects. 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 generates the rules for you.
Here's an overview on what this generator 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
^ | 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.