Forum Moderators: phranque

Message Too Old, No Replies

Trouble with rewriteRule

Backreference issue

         

jcounts

11:02 pm on Jul 5, 2007 (gmt 0)

10+ Year Member



we have a lot of folders and each has an index.php file which draws dynamic content from a database. So people just access the folder with a /sunshine (or whatever)

Now we want to display different content based on the folder name.

For example /sunshine_3 or /sunshine_4

I thought it would be fairly simple to just snip off the _3 or _4 and turn it into an argument such as /sunshine/index.php?door=3

Here is the relevant part of my .htaccess file

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/([a-z0-9]+)_([0-9])$ [NC]
RewriteRule ^(.*)$ /$1/index.php?door=$2

I need the condition to fail for URLS that don't have the trailing _3 or _4 etc and that works fine, but the backreference to $1 always incudes the _3 or _4 and I get a page connot be found error.

How do I snip off the _3 or _4 so that $1 is only something like sunshine (not sunshine_3)

I have obsessed over this for about 5 days with no progress.

Thanks
John

jdMorgan

12:06 am on Jul 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's no need for a RewriteCond, and if you do use one, you must use %1 and %2 to back-reference RewriteCond pattern matches instead of $1 and $2 as used to back-reference RewriteRule matches.

All you need, based on what you posted, is something like this:


RewriteEngine on
RewriteRule ^([a-z0-9]+)_([0-9]+)$ /$1/index.php?door=$2 [NC,L]

Jim

jcounts

12:29 am on Jul 6, 2007 (gmt 0)

10+ Year Member



Wonderful...thank you so much