How to 301 Redirect Dynamic URLs With Your .Htaccess File

1 year ago 189
ARTICLE AD BOX

Editor’s note: This usher was primitively published successful November 2008. It has since been updated for accuracy and to bespeak modern practices.

If you’ve ever had to bash URL redirects for 1 of your websites, you’ve astir apt been overwhelmed with each of the antithetic ways to bash it. This codification tin beryllium confusing and, arsenic a result, tin beryllium highly frustrating to fig out. 

However, erstwhile you do acceptable up URL redirects respective times and go acquainted with the .htaccess file, the process becomes reasonably easy.

In this guide, I’ll locomotion you done a fewer dynamic HTTP redirections, based connected our agency’s enactment with dozens of eCommerce clients.

But, first, let’s spell implicit immoderate important inheritance information.

What is simply a 301 Redirect?

A 301 redirect is simply a imperishable redirect — meaning that, erstwhile a idiosyncratic tries to entree an aged oregon outdated URL, the redirect sends their browser to another, much applicable webpage instead.

Redirects are evidently important for idiosyncratic experience; aft all, dormant URLs are hardly charismatic to visitors. 

But they’re besides cardinal for your SEO strategy, arsenic well.

When you acceptable up a 301 redirect (or immoderate redirect, for that matter), you archer a hunt engine’s crawlers that the archetypal contented has been moved. That way, the hunt motor tin amended support its indexes and proceed to service applicable contented to its users.

What is simply a Dynamic URL?

A dynamic URL occurs erstwhile the contents of a webpage alteration based connected adaptable parameters provided to the server, creating an idiosyncratic unsocial URL each time. 

This benignant of URL is often utilized connected eCommerce websites to make class pages wherever a idiosyncratic tin filter down the merchandise database to look astatine items with circumstantial features. 

For example, connected a covering website, you mightiness beryllium looking astatine the t-shirt page, but privation to look astatine a circumstantial color, size, oregon cut. A dynamic URL would beryllium utilized to physique a t-shirt class leafage with your circumstantial filters. 

You tin spot an illustration successful the screenshot from DollarDays.com below, wherever we’ve filtered footwear products by features and terms to make a unsocial URL:

DollarDays.com screenshot of footwear  class  page, with an arrow pointing to the dynamic URL generated by filter selections

This is opposed to a static URL, successful which the contented and URL of a leafage don’t alteration and stay the same.

What is an .htaccess File?

An .htaccess file is simply a website record that controls the configuration of your website. By making edits wrong this record (rather than successful the server configuration directly), you tin marque important changes to your site.

Keep successful mind: Because you’ll straight edit the codification successful this file, it’s casual to marque a mistake that ripples crossed your tract and impacts your users’ experience. So, we urge utilizing caution and paying adjacent attraction to item erstwhile editing this file.

When to Use a 301 Redirect 

In short, a 301 redirect should beryllium utilized erstwhile a URL is nary longer needed oregon is being removed from the site.

Let’s accidental you tally an eCommerce website and will nary longer connection a definite product, whether due to the fact that you’re changing your offerings oregon due to the fact that the merchandise is retired of banal and volition ne'er return. Using a 301 redirect connected this URL volition bespeak to users and hunt engines that this merchandise volition nary longer beryllium offered connected your tract — and that they should alternatively notation to the caller leafage you are sending them to for akin products. 

You tin besides usage 301 redirects erstwhile you’re migrating to a caller eCommerce platform and, arsenic a result, your website URLs request to change. Again, you’d make a 301 redirect to archer users and hunt engines what the caller placement of the webpage volition beryllium connected the caller website.

How to Do a 301 Redirect successful .htaccess

The method you usage to implicit a 301 redirect volition vary, depending connected the server that your website runs on. In astir cases, the .htaccess method is utilized connected Apache servers, but you tin usage it connected different servers, arsenic well.

In addition, due to the fact that determination are truthful galore antithetic server configurations, you mightiness person to effort respective methods to find the 1 that works champion for your site. In the examples below, I stock the strategies that I’ve successfully utilized for our clients’ sites.

1. Redirecting a Single URL

For 1 of our clients, I wanted to redirect https://www.example.com/page.php?id=68 to https://www.example.com/content/page. After attempting the tried-and-true processes, I had to experimentation a bit.

Using regex (regular expression), this is the format that yet worked for me:

RewriteCond %{QUERY_STRING} ^id=1$

RewriteRule ^$ https://www.example.com/content/page? [R=301,L]

To usage the aforesaid strategy for your site, instrumentality your URLs and plug them into the due areas outlined successful the representation below:

//www.example.com/page.php?id=68. page.php is labeled A. id=68 is labeled B. New U R L: http://www.example.com/content/page. The caller   U R L is labeled C. Complete Redirect: RewriteCond %{Query_String} ^id=68$. id=68 is labeled B. RewriteRule ^page\.php$http://www.example.com/content/page?[R=301,L]. page\.php is labeled A (add a \ earlier  the .php). http://www.example.com/content/page is labeled C. ? is labeled Add a "?" instantly  aft  the caller   U R L.

Note that aft the question people successful the URL, you’ll find an ID. While this mightiness look antithetic for your URL, you tin alteration the input successful the redirect accordingly. 

For example, your URL mightiness beryllium page.php?page_num=68. If this is the case, you’ll conscionable replace id=68 with page_num=68 successful the redirect code.

When utilizing this method, don’t hide to adhd the question people to the extremity of the caller URL that you’re trying to redirect to.

2. Redirecting a Group of URLs

What if you’re redirecting respective dynamic URLs astatine once?

For a due dynamic redirect, your .htaccess should look similar this:

RewriteCond %{QUERY_STRING} ^id=1$

RewriteRule ^page.php$ https://www.example.com/content/page? [R=301,L]

RewriteCond %{QUERY_STRING} ^id=72$

RewriteRule ^page.php$ https://www.example.com/content/page2? [R=301,L]

RewriteCond %{QUERY_STRING} ^id=46$

RewriteRule ^page.php$ https://www.example.com/content/page3? [R=301,L]

3. Redirecting URLs with Multiple IDs

If you person URLs with aggregate IDs, you tin simply grow the expression:

Old URL: https://www.example.com/page.asp?id=4&mscsid=49

New URL: https://www.example.com/page.asp?id=4&mscsid=49

RewriteCond %{QUERY_STRING} ^id=4&mscsid=49$

RewriteRule ^page.asp$ https://www.example.com/content/page1? [R=301,L]

4. Redirecting a Group of URLs successful One Statement

If you privation to streamline your redirects, you mightiness take to redirect galore URLs wrong a azygous statement. Here’s what you tin do:

Old URL: https://www.example.com/page.php?id=1111

New URL: https://www.example.com/content/page_name

RedirectMatch 301 ^page.php?id=(.*).htm$ https://www.example.com/content/$1.html

5. Redirecting a URL without an ID

What if your older URLs don’t person IDs? How tin you redirect them?

Don’t panic; determination is simply a solution. 

Old URL: https://www.example.com/?/page/55c9/

New URL: https://www.example.com/newpage.php/page/55c9/

RewriteCond %{QUERY_STRING} ^/page/55c9/$

RewriteRule ^$ https://www.example.com/newpage.php/page/55c9/? [R=301,L]

Note: Whichever method you usage to implicit your 301 redirects, if your URL includes a hash (#), it volition not redirect properly.

Other Helpful Resources

To larn much astir dynamic HTTP redirections and URL rewrites, we urge the pursuing resources and tutorials:

In addition, Wordpress offers aggregate plugins for editing your .htaccess file.

As you’re implementing your redirects, if you can’t find an reply to your unsocial situation, I urge asking the folks implicit astatine WebmasterWorld.com, successful the Apache forum. In my experience, that assemblage is large astatine troubleshooting immoderate redirect errors you mightiness encounter.

If redirecting dynamic URLs is portion of a pre- oregon post-migration process for your eCommerce site, our SEO squad is ever blessed to help. Contact america today for a escaped strategy connection to minimize your mislaid postulation and amended your integrated performance.