Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite n-levels deep directory

urgent please!

         

Anyango

6:04 pm on Oct 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello Folks!

should be a pretty simple thing to do but am kinda stuck.

lets say we have a url

index.php?directory=partners&file=test.html

we can simply rewrite this url using

RewriteRule ^([^/]+)/(.*\.html)$ index.php?directory=$1&page=$2 [L]

now this works for either top level or one level deep

for example , this works fine for

[anyurl.com...]

and

[anyurl.com...]

problem is, that i would like that rule to cater for any number of directories in the path

i.e

1)http://anyurl.com/dir1/anyothersub/test.html

2)http://anyurl.com/dir1/sub1/sub2/sub3/test.html

and this should be sent to index.php like this

[anyurl.com...]
>>index.php?directory=dir1/anyothersub&file=test.html

[anyurl.com...]
>>index.php?directory=dir1/sub1/sub2/sub3&file=test.html

and [anyurl.com...]

should simply be

index.php?directory=&file=test.html

urgent assistance will be highly appreciated and regarded.
I hope this description is clear, Thanks in Anticipation.

Kami

jd01

7:25 pm on Oct 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should be able to do this fairly easily by adding a regex for additional directories and making them optional...

RewriteRule ^([^/]+/)?([^/]+/)?([^/]+/)?([^/]+/)?([^/]+)/([^.]+\.html)$ index.php?directory=$1$2$3$4$5&page=$6 [L]

I also adjusted your last expression to "anything that is not a dot" to save processing and for the sake of jdMorgan's no .* none of the time crusade =)

Hope this helps.

Justin

Anyango

7:35 pm on Oct 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey Justin

Thank you very much for your reply and code, i ll implement that.

isnt this possible to have a generic regex so that i dont need to specify how many directories code should expect, actually the scenario is as this

There is a directory system in which new categories and sub categories can be added at any time, so for example there is a main category

forums and currently it has 2 sub categories

forums
apache
mysql

one url would be like

forums/apache/index.html

now if few days after, someone adds a new sub directory for apache and it becomes


forums
apache
.htacess
security
mysql

then the same regex could handle following url

forums/apache/security/index.html

Thanks for your words
Kami

jd01

7:52 pm on Oct 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sure, but I'm not all the way awake yet, and you asked for a quick answer... all you will need to do is anjust the regular expression, so it catches everything up to the last /, then catches everything after the / separately -- can be done easily, but inefficiently with (.*), so usually better to find another way like [^.]=is not a dot, OR [^\ ]=is not a space, etc.

I am sure you will come up with something.

Justin

jdMorgan

1:31 am on Oct 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about:

RewriteRule ^([^/]*/)+([^.]+\.html)$ index.php?directory=$1&page=$2 [L]

Jim

Anyango

5:32 am on Oct 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Justin and Jim!

Thank you very much for your assistance , you guys have always helped me alot.

Jim , i tested the regex you posted, looks like it only gets the name of last directory requested and the filename, it doesnt get names of all the directories requested, for example

for a url like

anyurl.com/dev/test/test2/test3/index.html

it would say

it is populating variables like
directory=test3/
page=index.html

it would be lovely if a little tweaking with that regex could make it populate variables for the same url like this

anyurl.com/dev/test/test2/test3/index.html
[
directory=dev/test/test2/test3
page=index.html
]

anyurl.com/dev/test.html

[
directory=dev
page=test.html
]

Thanks again to Both of you.

Kami

jdMorgan

1:04 pm on Oct 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Strange, I didn't test it but apparently, it's repeatedly replacing $1 with each new instance of <anything/> that it finds.

So how about:

RewriteRule ^(([^/]*/)+)([^.]+\.html)$ index.php?directory=$1&page=$3 [L]

(To resolve back-reference assignements, count left-parentheses.)

Jim

Anyango

8:09 pm on Oct 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks a Million, Brothers!

Works as expected.

Ciao

daily

9:49 pm on Nov 6, 2005 (gmt 0)

10+ Year Member



I'm struggling with a same kind of problem. I would like to have just n-level deep directory, so that we accept only alphanumerical values and -. So we could have a url like this: [anyurl.com...] (last slash should be optional)

RewriteRule ^(([^/]*/?)+)$ index.php?url=$1 this is the best I have managed to get to work so far. The problem is that it also accepts other than alphanumerical values and -

daily

10:04 pm on Nov 6, 2005 (gmt 0)

10+ Year Member



This seems to work:
RewriteRule ^((([-a-z0-9]+)(/?))+)$ index.php?url=$1 [NC]

Is this the right way to do it?

How would I have to change this strip out the last slash. So that this request
[anyurl.com...]
would output same as this
[anyurl.com...]

index.php?url=dir-1/dir-2/this-page

jd01

10:46 pm on Nov 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It looks like you got pretty close...

RewriteRule ^((([-a-z0-9]+)(/?))+)$ index.php?url=$1 [NC]

A little more efficient:
RewriteRule ^([-a-z0-9]+/?)+$ index.php?url=$1 [NC,L]

Always good to use the L flag (unless you know you do not need it) -- will save some processing.

The / *should* be optional here and the pattern should match if repeated 1 or more times... Have not had time to test it, so please let us know how it goes.

Hope this helps.

Justin

daily

12:11 am on Nov 7, 2005 (gmt 0)

10+ Year Member



It doesn't work. It only matches the last part. So
this [anyurl.com...] becomes
index.php?url=the-page

when it should become
index.php?url=dir-1/dir-2/the-page/

jd01

3:41 am on Nov 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Interesting...

Looks like the same problem jdMorgan was having earlier in this thread. I still do not really have time to test, just point in a direction. I would try these regular expressions and some other variations of the one posted by jdMorgan:

(([a-z0-9-]*/?)+)

OR

(([a-z0-9/-]*)+)/?$

Let us know how this goes...

Justin

daily

10:30 pm on Nov 9, 2005 (gmt 0)

10+ Year Member



(([a-z0-9/-]*)+)/?$
This doesn't work because it allows slash after slash.

The first one might just work. I have to test it out.