开发者

Url rewriting only directories

Currently my root web directory has a .htaccess which redirects to my application folder like such:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^$   app/    [L]
    RewriteRule (.*) app/$1  [L]
</IfModule>

And inside the /app directory I have another .htaccess which has the following:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?url=$1 [PT,L]

</IfModule>

At this point, back in my PHP, I can use $_GET['url'] to obtain the requested URL. This is all fine and 开发者_开发技巧good. However, what I am looking to do, is only have it rewrite if the requested url is a directory, and if the requested url is to a file, it will actually redirect to that file for viewing. I am not sure if this can be done purely through the .htaccess, or if there is a way in php to do a is_file check, and then do a header redirect to the file?

Right now if I do a is_file check, and do a header redirect, I get placed in a loop, because the htaccess in the webroot obviously wants to redirect to the /app directory

What would be the best way to go about this?


I'm still not 100% clear on what exactly is required, hopefully this is what you are asking. If not -- let me know.

Add one more rule into website root .htaccess to tell Apache to ignore requests to existing files:

<IfModule mod_rewrite.c>
    RewriteEngine On

    # do not rewrite requests to existing files
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule . - [L]

    RewriteRule ^$ app/ [L]
    RewriteRule (.+) app/$1 [L]
</IfModule>

Obviously, if file was requested (e.g. example.com/test.php) but it does not exist, the rewrite will still occur.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜