Reputation: 99
I have been looking for an answer for a few hours now, so sorry if this was asked a ton of times, I missed it.
I basically want to make a rewrite to ignore the first directory. That first dir in the path will be different so I thought I could use a regex. But my regex is matching all the way to the file name:
RewriteRule ^([a-z]+)?/(.+)$ $2 [L]
this works if I am one level deep:
http://test.domain.com/one/index.php
I get the actual index page of the root. Which is what I want. but if I were to go deeper:
http://test.domain.com/one/two/anotherfile.php
I get a message saying /anotherfile.php was not found, because it is looking in the root for it. So it seems my regex is not stopping after the last [a-z]. I appreciate any help.
This is Apache2 if that matters at all.
Upvotes: 1
Views: 2922
Reputation: 143856
The rewrite engine repeats all the rules until the URI is the same before and after an iteration through the rules. Given the rule, RewriteRule ^([a-z]+)?/(.+)$ $2 [L]
, and the input: http://test.domain.com/one/index.php
, this is what's happening:
one/index.php
^([a-z]+)?/(.+)$
/index.php
/index.php
) doesn't match old URI (/one/index.php
) and is run back through the rewrite rules^([a-z]+)?/(.+)$
/index.php
) is same as old URI before being run through the rewrite engine (/index.php
), rewriting stopsResulting URI is /index.php
But with the input: http://test.domain.com/one/two/anotherfile.php
one/two/anotherfile.php
^([a-z]+)?/(.+)$
/two/anotherfile.php
/two/anotherfile.php
) doesn't match old URI (/one/two/anotherfile.php
) and is run back through the rewrite rulestwo/anotherfile.php
)^([a-z]+)?/(.+)$
/anotherfile.php
/anotherfile.php
) doesn't match old URI (/two/anotherfile.php
) and is run back through the rewrite rulesanotherfile.php
)^([a-z]+)?/(.+)$
/anotherfile.php
) is same as old URI before being run through the rewrite engine (/anotherfile.php
), rewriting stopsResulting URI is /anotherfile.php
The [L]
doesn't stop the resulting URI from being put back through the rewrite engine, it just stops the rewriting process for the current iteration. You need to change the regular expression or add some kind of rewrite condition. One possibility is adding RewriteRule ^[a-z+]/([a-z]+)?/(.+)$ $2 [L]
before the one that you have so that 2 deep directories get handled separately.
Upvotes: 1
Reputation: 530
Hmmmm...
Throwing some thoughts out there:
I thought there might be recursive RewriteRule
-ing... but I'm not sure, since you put the 'last' rule in there. However, this is the only logical thing I think could explain what is happening.
If Apache isn't stopping, I'm not sure what recourse you have. Perhaps RewriteCond
, DPI
(DPI discards PATH_INFO
), analyzing THE_REQUEST
, or more?
Sorry I couldn't be of more help. I hope you worked around this in the past year.
Upvotes: 0