homepage Welcome to WebmasterWorld Guest from 54.166.10.100
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
RewriteMap & Rewrite Rule - dynamic urls to static urls
Brian Harris




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

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:

www.mydomain.com/?p=1
www.mydomain.com/?p=2
etc...

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:

www.mydomain.com/some-category/the-article-title.html

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]

pids.txt:
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.

 

jdMorgan




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

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]

/path_to_document_root/pids.txt:
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.

Jim

Brian Harris




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

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]

pids.txt
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->
http://www.example.com/?p=1

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.

Brian

jdMorgan




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

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.

Jim

Brian Harris




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

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!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved