How to 301 Redirect Dynamic URLs With Your .Htaccess File

How to 301 Redirect Dynamic URLs With Your .Htaccess File

Editor’s note: This guide was originally published in November 2008. It has since been updated for accuracy and to reflect modern practices.

If you’ve ever had to do URL redirects for one of your websites, you’ve probably been overwhelmed with all of the different ways to do it. This code can be confusing and, as a result, can be extremely frustrating to figure out. 

However, once you do set up URL redirects several times and become familiar with the .htaccess file, the process becomes fairly easy.

In this guide, I’ll walk you through a few dynamic HTTP redirections, based on our agency’s work with dozens of eCommerce clients.

But, first, let’s go over some important background information.

What is a 301 Redirect?

A 301 redirect is a permanent redirect — meaning that, when a user tries to access an old or outdated URL, the redirect sends their browser to another, more relevant webpage instead.

Redirects are obviously important for user experience; after all, dead URLs are hardly attractive to visitors. 

But they’re also key for your SEO strategy, as well.

When you set up a 301 redirect (or any redirect, for that matter), you tell a search engine’s crawlers that the original content has been moved. That way, the search engine can better maintain its indexes and continue to serve relevant content to its users.

What is a Dynamic URL?

A dynamic URL occurs when the contents of a webpage change based on variable parameters provided to the server, creating an individual unique URL each time. 

This type of URL is often used on eCommerce websites to create category pages where a user can filter down the product list to look at items with specific features. 

For example, on a clothing website, you might be looking at the t-shirt page, but want to look at a specific color, size, or cut. A dynamic URL would be used to build a t-shirt category page with your specific filters. 

You can see an example in the screenshot from DollarDays.com below, where we’ve filtered shoe products by features and price to generate a unique URL:

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

This is opposed to a static URL, in which the content and URL of a page don’t change and remain the same.

What is an .htaccess File?

An .htaccess file is a website file that controls the configuration of your website. By making edits within this file (rather than in the server configuration directly), you can make important changes to your site.

Keep in mind: Because you’ll directly edit the code in this file, it’s easy to make a mistake that ripples across your site and impacts your users’ experience. So, we recommend using caution and paying close attention to detail when editing this file.

When to Use a 301 Redirect 

In short, a 301 redirect should be used when a URL is no longer needed or is being removed from the site.

Let’s say you run an eCommerce website and will no longer offer a certain product, whether because you’re changing your offerings or because the product is out of stock and will never return. Using a 301 redirect on this URL will indicate to users and search engines that this product will no longer be offered on your site — and that they should instead refer to the new page you are sending them to for similar products. 

You can also use 301 redirects when you’re migrating to a new eCommerce platform and, as a result, your website URLs need to change. Again, you’d create a 301 redirect to tell users and search engines what the new placement of the webpage will be on the new website.

How to Do a 301 Redirect in .htaccess

The method you use to complete a 301 redirect will vary, depending on the server that your website runs on. In most cases, the .htaccess method is used on Apache servers, but you can use it on other servers, as well.

In addition, because there are so many different server configurations, you might have to try several methods to find the one that works best for your site. In the examples below, I share the strategies that I’ve successfully used for our clients’ sites.

1. Redirecting a Single URL

For one 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 experiment a bit.

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

RewriteCond %{QUERY_STRING} ^id=1$

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

To use the same strategy for your site, take your URLs and plug them into the appropriate areas outlined in the image below:

Three sections titled Original U R L, New U R L, Complete redirect as follows. Original UR L: http://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 new 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  before the .php). http://www.example.com/content/page is labeled C. ? is labeled Add a "?" immediately after the new U R L.

Note that after the question mark in the URL, you’ll find an ID. While this might look different for your URL, you can change the input in the redirect accordingly. 

For example, your URL might be page.php?page_num=68. If this is the case, you’ll just replace id=68 with page_num=68 in the redirect code.

When using this method, don’t forget to add the question mark to the end of the new URL that you’re trying to redirect to.

2. Redirecting a Group of URLs

What if you’re redirecting several dynamic URLs at once?

For a proper dynamic redirect, your .htaccess should look like 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 have URLs with multiple IDs, you can simply expand 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 in One Statement

If you wish to streamline your redirects, you might choose to redirect many URLs within a single statement. Here’s what you can 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 have IDs? How can you redirect them?

Don’t panic; there is 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 use to complete your 301 redirects, if your URL includes a hash (#), it will not redirect properly.

Other Helpful Resources

To learn more about dynamic HTTP redirections and URL rewrites, we recommend the following resources and tutorials:

In addition, WordPress offers multiple plugins for editing your .htaccess file.

As you’re implementing your redirects, if you can’t find an answer to your unique situation, I recommend asking the folks over at WebmasterWorld.com, in the Apache forum. In my experience, that community is great at troubleshooting any redirect errors you might encounter.

If redirecting dynamic URLs is part of a pre- or post-migration process for your eCommerce site, our SEO team is always happy to help. Contact us today for a free strategy proposal to minimize your lost traffic and improve your organic performance.

https://www.goinflow.com/blog/setting-up-301-redirects-for-dynamic-urls/

#seo #seoservices #SEOServicesCompany #SeoServiceProvider #seoexpert #digitalmarketingtips #marketing #socialmediamarketing #socialmedia #webdesign #blogger #onlinemarketing #marketingdigital #contentmarketing #website #searchengineoptimization #advertising #internetmarketing #marketingstrategy #entrepreneur #digitalmarketingagency #ecommerce #webdevelopment #digital #design #marketingtips #sem #websitedesign #smallbusiness #graphicdesign

Scroll to Top