Jump to content

Regex in htaccess, folder structure?


Jessica

Recommended Posts

I have the following rules in my htaccess:

 

RewriteRule ^journals([A-Za-z0-9_/]+)$ /journals/base.php [NC]

RewriteRule ^quadoo([A-Za-z0-9_/]+)$ /quadoo/base.php [NC]

RewriteRule ^scrapbook([A-Za-z0-9_/]+)$ /scrapbook/base.php [NC]

RewriteRule ^CMS([A-Za-z0-9_/\-]+)$ /CMS/base.php [NC]

 

I wanted to simplify it, but what I tried didn't work:

 

RewriteRule ^[A-Za-z0-9_]+/([A-Za-z0-9_/\-]+)$ /$1/base.php [NC]

 

Help?

Link to comment
https://forums.phpfreaks.com/topic/38103-regex-in-htaccess-folder-structure/
Share on other sites

^journals([A-Za-z0-9_/]+)$

^ ### BOL

journals ### literal

( ### Start capture

[A-Za-z0-9_/]+ ### Match one or more of any of these characters; there may not be a forward slash at all!

) ### End capture

$ ### EOL

 

^[A-Za-z0-9_]+/([A-Za-z0-9_/\-]+)$

^ ### BOL

[A-Za-z0-9_]+ ### Match one or more of any of these characters

/ ### Match a forward slash

( ### Start capture

[A-Za-z0-9_/\-]+ ### Match one or more of any of these characters

) ### End capture

$ ### EOL

If I use:

RewriteRule ^([A-Za-z0-9_]+)/([A-Za-z0-9_/\-]+)$ /$1/base.php [NC]

None of them work.

So I added

RewriteRule ^CMS([A-Za-z0-9_/\-]+)$ /CMS/base.php [NC]

If you go to http://www.grady.us/CMS/ that works.

But if you go to http://www.grady.us/quadoo/, you'll just see the almost-empty index file, not base.php

These are failing to match because they do not have data after the slash:

^[A-Za-z0-9_]+/([A-Za-z0-9_/\-]+)$

^ ### BOL

[A-Za-z0-9_]+ ### This is matching "CMS", for example.

/ ### This is matching the "/".

( ### Start capture

[A-Za-z0-9_/\-]+ ### This cannot match anything, therefore failing.

) ### End capture

$ ### EOL

If you do not require data after the slash, change + to *.

Because you weren't matching two sets of data, only one. Review the first comparison I posted line by line, and go through it character by character.

 

The first pattern says "Find 'journals' then one or more characters, which could be a slash"; whereas the second says "Find one or more characters, not including the slash, then a slash, then one or more characters, which could be a slash."

 

That extra step makes all the difference.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.