Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

RewriteMap & Rewrite Rule - dynamic urls to static urls


Brian Harris

6:56 pm on Feb 1, 2007 (gmt 0)

5+ Year Member

So here is my situation, currently running a Wordpress blog, but more or less using it as a CMS. my current server doesn't support mod_rewrite so i have a hundred or so posts that look like this:


I'm moving to a new WAMP server running drupal 4.7.5 which does support mod_rewrite. i will be manually moving old content to drupal. The new url format will be something like this:


My question is, what is the best way to handle 301 redirects to the new content. I've been reading up on mod_rewrite for about a week now and can't really find anything that works well.

This works for redirecting one instance, but since i have a hundred or so, two lines for one redirect seems a little much:

RewriteCond %{query_string} ^feed=rss2&cat=34$
RewriteRule ^/$ /feed/? [R=301,L]

But i came across this and thought that it may work for all my?p= variables.

RewriteMap pages txt:pids.txt
RewriteRule ^(.*)$?p=${pages:$1¦0} [R=301,L]

testing/test 1
testing2/test2 2

when i use the above RewriteMap and RewriteRule, and restart apache, apache doesn't start and gets hungup on "RewriteMap pages txt:pids.txt". I have tried /pids.txt and pids.txt (the text file is located in the public webroot, and this is a windows server running apache so i'm a little confused.

Any help would be greatly appreciated.


2:06 pm on Feb 2, 2007 (gmt 0)

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

From your examples, I can't really tell what the relationship between the old 'dynamic' URLs and the new 'static' URLs is. However, note the various changes below.

I assume that the feed name is always "rss" and that it is only the category that changes. However, you can probably adapt this example if that's not true.

RewriteMap pages txt:/same-path-as-used-for-document_root/pids.txt
RewriteCond %{QUERY_STRING} ^feed=rss&cat=([0-9]+)$
RewriteRule ^/$ http://www.example.com/${pages:%1¦NULL} [R=301,L]

1 category-1/article-2.html
2 category-1/article-3.html
3 category-2/article-7.html
19 category-3/article-21.html

Replace the broken pipe "¦" character above with a solid pipe character before use; Posting on this forum modifies the pipe character.

The intent here is to redirect the old dynamic URLs to the new static ones, and then let the existing CMS internal rewrites take care of them just as they would if the new static URL was directly requested.


Brian Harris

8:12 pm on Feb 2, 2007 (gmt 0)

5+ Year Member

Hey Jim, Thanks for your help but I'm still running into some problems. I copied your exact code and made a couple small changes.

RewriteMap pages txt:c:/wamp/Apache2/conf/pids.txt
RewriteCond %{QUERY_STRING} ^p=([0-9]+)$
RewriteRule ^/$ http://www.example.com/${pages:%1¦NULL} [R=301,L]

1 category-1/article-2.html
2 category-1/article-3.html
3 category-2/article-7.html
4 category-3/article-21.html

when i go to
[subdomain.mydomain.com...] Redirects to->

so when i change www.example.com to my actual address the browser stops the request because it is caught in an endless loop.

just to make sure we are on the same page here,
when [subdomain.mydomain.com...] is hit it should then goto
[subdomain.mydomain.com...] set in pids.txt)

Am i referencing the pids.txt file correctly? This is hosted on a windows box, w/ apache2

Thanks for your help.



8:41 pm on Feb 2, 2007 (gmt 0)

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

Two problems, the easy one first:

For some reason, I always forget to clear the query string, and I did it again here.

Append a question mark to the substitution URL to clear the query string:

RewriteMap pages txt:c:/wamp/Apache2/conf/pids.txt
RewriteCond %{QUERY_STRING} ^p=([0-9]+)$
RewriteRule ^/$ http://www.example.com/${pages:%1¦NULL}[b]?[/b] [R=301,L]

We know that the rule is matching and executing, because you are getting a redirect. However, it seems that the map lookup is having no effect -- That is, it can't find the map file or the mapped value, so it is using the "NULL" value.

Map files are sometimes a bit difficult to get working. Things like path differences and such are critical. I don't run Apache on Windows, so I can't help much with that part, but try putting that path into "windows format" with backslashes instead of forward slashes, and leaving out the drive name. You may then find the "just right" form that you need. I've never had a problem with that as long as the RewriteMap path matches or resembles the path specified for the server DocumentRoot.

Make sure the file permissions on your Map file permit read access from the Web context, and be sure that the Map file contents are written with a plain-text editor using ASCII characters and standard LF or CRLF line-enders.

Be aware that this new rule will only execute if your query string is exactly p=<numbers>. Your initial example had multiple parameters, so be sure to account for that difference.

Restart the server after any map or config file changes.

And again, beware of the broken pipe character problem mentioned in my first post.


Brian Harris

9:02 pm on Feb 2, 2007 (gmt 0)

5+ Year Member

you rock!

it turns out it was the question mark. And I also was referencing the text file correctly.

just for anyone else who is looking for a similar solutions here is the working code:

RewriteMap pages txt:c:/wamp/Apache2/conf/pids.txt
RewriteCond %{QUERY_STRING} ^p=([0-9]+)$
RewriteRule ^/$ /${pages:%1¦NULL}? [R=301,L]

Thanks again!


Featured Threads

Hot Threads This Week

Hot Threads This Month