Mod_Rewrite Help Wanted, Apply Within
March 2, 2007 9:27 AM   Subscribe

I am in mod_rewrite hell. What a syntax! Can someone smarter than me have a look and tell me I'm not crazy? Thanks!

Okay, mod_rewrite is working and respecting my .htaccess file, so there's no installation problem. I wish to shorten URLs in this form:


www.mysite.com/dynamicpages/newstory.shtml?shownews.tmpt&showstory=122


To something like this:


www.mysite.com/news?122


So that users can type the latter and get the same results they would get from the "expanded" first version.

I know this is exactly what mod_rewrite is for, but for the life of me I can't get it to produce anything other than nasty looping errors like "Too many redirects."

Can someone demonstrate the no-doubt three-liner I need? Thanks.
posted by rokusan to Computers & Internet (8 answers total) 2 users marked this as a favorite
 
Try something like this:

RewriteRule ^news\?(+[0-9])$ /newstory.shtml?shownews.tmpt&showstory=$1 [L]

I'm probably wrong though. :)
posted by afx114 at 9:36 AM on March 2, 2007


How about...

^news/(\d+)/?$ dynamicpages/newstory.shtml?shownews.tmpt&showstory=$1

That should turn www.mysite.com/news/122 into the correct url.
posted by the jam at 9:36 AM on March 2, 2007


You may get some use from this similar question; for one thing, the treatment of the post '?' contents needs special attention, I think.
posted by cortex at 9:40 AM on March 2, 2007


If you're getting the too many redirects error, check to make sure that your target doesnt have a / in front. (I got the tip from here because I was having the same problem this morning)
posted by SirOmega at 9:41 AM on March 2, 2007


Do you have to escape special characters?
posted by loiseau at 11:27 AM on March 2, 2007


I use something very similar ... here's a paraphrase of my .htaccess (using your pages)

Options +FollowSymlinks
RewriteEngine on
#if file exists, give it to them.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*) $1 [L]
RewriteBase /dynamicpages
#pages
RewriteRule ^news\?(+[0-9])$ newstory.shtml?shownews.tmpt&showstory=$1 [NC,L]


what's up with the shownews.tmpt in the query string? that looks way weird to me.
posted by fishfucker at 11:42 AM on March 2, 2007


From the mod_rewrite docs: The Pattern will not be matched against the query string. To do this, you must use a RewriteCond with the %{QUERY_STRING} variable.

So use something like:

RewriteCond %{QUERY_STRING} ^(\d+)$
RewriteRule ^news dynamicpages/newstory.shtml?shownews.tmpt&showstory=%1

posted by Freaky at 12:35 PM on March 2, 2007


cool, freaky. never knew that.
posted by fishfucker at 12:50 PM on March 2, 2007


« Older India drug laws   |   I need your recipes. Newer »
This thread is closed to new comments.