Forum Moderators: phranque
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
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
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
I am sure you will come up with something.
Justin
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
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 -
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
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
when it should become
index.php?url=dir-1/dir-2/the-page/
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