Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Redirecting to static URL when dynamic URL is requested

Directing a GET query to a static URL

1:49 pm on Jun 12, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 18, 2003
posts: 128
votes: 0

I've been able to get my dynamic pages disabling correctly under static URLs, and I'm trying to ensure these static URLs are used only (duplicate content issues etc).

I was hoping someone could point me in the right direction with what I have in my .htaccess file.

Based on jdMorgan's explanation of the rewrite [webmasterworld.com], I've got the following:

# Enable mod_rewrite, start rewrite engine
Options +FollowSymLinks
RewriteEngine on
# Internally rewrite search engine friendly static URL to dynamic filepath and query
RewriteRule ^(.*)view/(.*)/(.*)$ $1cgi-bin/view.pl?order=$2&orderdirection=$3

# Externally redirect client requests for old dynamic URLs to equivalent new static URLs
rewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /view\.pl\?order=([^&]+)&orderdirection=([^\ ]+)\ HTTP/
rewriteRule ^view\.pl$ http://www.example.com/view/%1/%2? [R=301,L]

These rules have www.example.com/view/DISK/ASC retrieving it's content from www.example.com/cgi-bin/view.pl?order=DISK&orderdirection=ASC correctly, but when the script is called directly it doesn't direct the user to the static page for the content.

Is there anything obvious that I've messed up with the .htaccess formatting?

I understand the "power" of regular expressions, but reading and writing .htaccess files feels like I'm on one of those code obsfucation contents...

My thanks in advanced to anyone providing assistance.

2:07 pm on June 12, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
votes: 0

If "view.pl" is located in /cgi-bin, then you'll need to include "cgi-bin" in both patterns of your second rule.

Your first rule is using terribly-inefficient patterns. Using ".*" multiple times in one pattern leads to horrible performance, because of the number of "trial" passes that the regex parser has to make to resolve them. It can also lead to very strange and unexpected matches, because ".*" matches anything.

I'd suggest re-coding that first rule as:

RewriteRule ^(.*)view/([^/]+)/([^/]+)$ $1cgi-bin/view.pl?order=$2&orderdirection=$3

This is better, but still leaves that initial ambiguous ".*" in place. Unfortunately, I cannot tell what is intended there. Regardless of the intent, however, I would suggest making that pattern more specific. If it is intended to be a single-level subdirectory path, the "([^/]+/)" is appropriate. If that subdirectory path is optional, then "([^/]+/)?" is appropriate, and if any number of subdirectories are to be acceptted, then "(([^/]+/)*)" will work.


2:17 am on June 13, 2008 (gmt 0)

Junior Member

10+ Year Member

joined:Feb 18, 2003
posts: 128
votes: 0

Thanks Jim, that was the key. I appreciate your assistance.