Blog

Dynamic 301 Redirect

  |   Blog

This is a solution to a very niche, but annoying problem. For SEO purposes, I was creating new URLs on an old-school dynamically generated site. So I needed a way to redirect from one dynamic url to another. The traditional .htaccess 301 redirect method doesn’t work in this situation. I found the solution here: https://www.webhostingtalk.com/showthread.php?t=659415

Here it is, for your htaccess file, after, of course, “RewriteEngine On”:

Instead of saying:
redirect 301 /old-directory/filename.php?item=old-page-name https://mysite.org/new-directory/filename.php?item=new-page-namethat doesn’t work

Say:
Option 1 (php file and directory stay the same):
RewriteCond %{QUERY_STRING} item=old-page-name
RewriteRule ^directory/filename.php$ /directory/filename.php?item=new-page-name [R=301,L]

Option 2 (directory and php file change, but page file name stays the same):
RewriteCond %{QUERY_STRING} item=page-name
RewriteRule ^directory/filename.php$ /new-directory/new-filename.php?item=page-name [R=301,L]