rewrite with subfolder

New to mod_rewrite? This is a good place to start.

rewrite with subfolder

Postby therealjlo » Fri Jun 26, 2009 1:39 pm

I just can't seem to get the hang of this. :(

I'm trying to rewrite from

http://www.example.com/thisfoldername/12345678.html

to
http://www.example.com/file.php?var=12345678

This is ONLY for the particular "thisfoldername". I want other names to point to different files, like

http://www.example.com/otherfoldername/12345678.html
to go to
http://www.example.com/otherfile.php?thisvar=12345678


And do you have a donate button somewhere? :)
therealjlo
 
Posts: 11
Joined: Tue Aug 05, 2008 9:37 am

Postby therealjlo » Fri Jun 26, 2009 2:19 pm

Sorry, here's the latest I've tried.

Code: Select all
Options +FollowSymLinks -MultiViews

RewriteEngine On

RewriteRule ^thisfoldername/[-a-z0-9]+)\.html?$ /file.php?var=$1 [QSA,L]

ErrorDocument 404 /index.php


All I get is a redirect to the index.php file
therealjlo
 
Posts: 11
Joined: Tue Aug 05, 2008 9:37 am

Postby richardk » Fri Jun 26, 2009 2:26 pm

Try
Code: Select all
Options +FollowSymLinks -MultiViews
ErrorDocument 404 /index.php

RewriteEngine On

RewriteRule ^thisfoldername/([0-9]+)\.html$  /file.php?var=$1          [QSA,L]
RewriteRule ^otherfoldername/([0-9]+)\.html$ /otherfile.php?thisvar=$1 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Like a dream

Postby therealjlo » Fri Jun 26, 2009 6:39 pm

Like a dream...

OK, so the big difference seems to be having the 404 first?

I took what I had above and just moved the 404 and it seems to work just fine. Of course, if I want to make sure it's numbers only do the "0-9" and not the "-a-z0-9".

Is that right?

And I'm serious about the donate button. You've saved me hours.
therealjlo
 
Posts: 11
Joined: Tue Aug 05, 2008 9:37 am

Postby richardk » Sun Jun 28, 2009 12:46 pm

OK, so the big difference seems to be having the 404 first?

The order doesn't (shouldn't) matter.

Of course, if I want to make sure it's numbers only do the "0-9" and not the "-a-z0-9".

Yes. Character classes.

There are two other problems with
Code: Select all
^thisfoldername/[-a-z0-9]+)\.html?$

It is missing a (. I would have expected that to cause a 500 error.
The ? at the end makes the L optional. (You probably don't want that.) Optional Items.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby therealjlo » Sun Jun 28, 2009 1:06 pm

Gotcha. Thanks for the help and the direction on those two issues. Didn't know what the ? was for so I left it in, and I did find some stuff on the character classes that allowed me to figure out how to add the option for dashes, underscores and spaces. Thanks again.
therealjlo
 
Posts: 11
Joined: Tue Aug 05, 2008 9:37 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 117 guests

cron