How To 301 Redirect Dynamic URLS


So you’ve built your website using a Content Management System such as WordPress and don’t want to display pages with dynamic URLs such as http://www.drostdesigns.com/?p=123. Search engines will still spider this type of URL however you miss out on including your main keywords plus the URL is difficult to spell and remember.

How To 301 Redirect Dynamic URLS Using Mod_Rewrite

WordPress solution

WordPress makes redirecting dynamic URLs very easy.

  • Login to your admin panel
  • Settings
  • Permalinks
  • Permalink Settings
  • Custom Structure (check the radio button)
  • Insert “/%postname%/” in the blank box (remove quotes)
  • Save Changes

This is the Mod_Rewrite code that automatically gets added to your .htacess file:


# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Instead of displaying http://www.drostdesigns.com/?p=123 it will display http://www.drostdesigns.com/

Do-It-Yourself

Example 1.

If you have a dynamic URL that looks like:

http://www.domain.com/shoes.php?sketchers

and want to make it look like this instead:

http://www.domain.com/shoes/sketchers

Add this code to the .htaccess file:


RewriteEngine On
RewriteRule ^/shoes/(.*)shoes.php?$1 [PT]

Why this works:
Anything after /shoes/ is removed.

Example 2.

If you have a dynamic URL that looks like:

http://www.domain.com/cgi-bin/product.cgi?camping=tent=large

and want to make it look like this instead:

http://www.domain.com/product/camping/tent

Add this code to the .htaccess file:


RewriteEngine On
RewriteRule ^ /product/([^/]*)/([^/]*)
/cgi-bin/product.cgi?camping=$1&topic=$2 [PT]

Why this works:
It requires that the requested URL look exactly like the URL that’s described in the pattern. A cleaner URL makes it easier for the search engines to spider.

How to 301 Redirect Old Dynamic URLs to New Static URLs Using .htaccess

Example 3.

If you have a dynamic URL that looks like:

http://www.domain.com/content.php?a=1&b=2

and want to redirect it to http://www.domain.com/content.html

Add this code to the .htaccess file:


RewriteCond %{HTTP_HOST} ^domain.com
RewriteCond %{QUERY_STRING} ^a=1&b=2$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/content.html [R=301,L]

Make sure you change the domain name and query string as needed. Now when you enter http://www.domain.com/content.php?a=1&b=2 in your browser address bar, it will redirect to http://www.domain.com/content.html. Search engines will view this as one page so you don’t have to worry about duplicate content.

Related Articles
How to Redirect a Website or Web Page and Preserve Your Rankings
301 Redirect for Multiple Domains
How to Redirect a Web Page Using a 301 Redirect
301 Redirect PHP – How to redirect a PHP page or website
301 vs 302 Redirect

**********************
Need help with the Design, Redesign or SEO of your website?
Go to: Website Design Maryland

Speak Your Mind

*