开发者

Mod rewrite into subdirectory

I have been having some trouble getting the following to work:

<IfModule mod_rewrite.c>
    DirectoryIndex index.php

    Options +FollowSymLinks

    RewriteEngine On

    # If the file is not found in web

    RewriteCond  web/$1 -f [NC] #<-- This line seems to be the problem.
    RewriteRule  ^(.*)$  web/$1  [L]

    # Then rewrite it to index.php

    RewriteRule ^(.*)$ index.php [L,QSA]
</IfModule>

The idea is to check if the file exists in the web directory, and if it doesn't, route the request to index.php.

The problematic line above seems to pick up the correct URL, but it does not recognize that the file exists.

For example:

http://localhost/img.gif --> /www/web/img.gif
http://localhost/subdir/file.doc --> /www/web/subdir/file.doc
http://localhost/user/add --> /www/index.php
http://localhost/invalidurl --> /www/index.php

However, I cannot get static resources to be served correctly, they are all routed to index.php.

I also want to keep all URLs relative; so I can make this code reusable without editing it.

The following .htaccess gives an Internal Server Error, if I visit img.gif:

<IfModule mod_rewrite.c>
    DirectoryIndex index.php

    Options +FollowSymLinks

    RewriteEngi开发者_运维百科ne On

    # If the file is not found in web

    #RewriteCond  web/$1 -f [NC] #<-- This line seems to be the problem.
    RewriteRule  ^(.*)$  web/$1  [L]

    # Then rewrite it to index.php

    #RewriteRule ^(.*)$ index.php [L,QSA]
</IfModule>

This .htaccess redirects to http://localhost/C:/absolute/path/to/web/img.gif, when visiting img.gif:

<IfModule mod_rewrite.c>
    DirectoryIndex index.php

    Options +FollowSymLinks

    RewriteEngine On

    # If the file is not found in web

    #RewriteCond  web/$1 -f [NC] #<-- This line seems to be the problem.
    RewriteRule  ^(.*)$  web/$1  [R]

    # Then rewrite it to index.php

    #RewriteRule ^(.*)$ index.php [L,QSA]
</IfModule>

My conclusion from this would be that it's getting the path correct, but some weirdness is causing it to do something completely weird (I don't even know why it has an internal server error - Should be 404 not found).


Ok I got it:

When you do a rewrite, then keep in mind that the rewritten URL is called internally. So you basically have to reprocess the .htaccess with mostly different values.

So in your example : http://localhost/img.gif is directed to http://localhost/web/img.gif and then to http://localhost/index.php by the last rule.

I would try this (replace the last rule by: )

RewriteCond %{SCRIPT_NAME} !^/web/
RewriteRule ^(.*)$ index.php [L]

(NB: [QSA] is not needed since you don't touch the query string, so it is passed as is.)

Edit : What about

# If the file is not found in web
RewriteCond  web/$1 !-f [NC]
# And we don't ask for /web/something at the beginning (Avoid infinite loops since you'll try to call web/image.gif and we don't want to test /web/web/image.gif and fail to index.php
RewriteCond %{SCRIPT_NAME} !^/web/
#Rewrite it to index.php
RewriteRule  ^(.*)$  index.php  [R,L]

# If the file is found in web 
RewriteCond  web/$1 -f [NC]
# And we don't ask for /web/something at the beginning (Avoid infinite loops since you'll try to call web/image.gif and we don't want to test /web/web/image.gif and fail to index.php
RewriteCond %{SCRIPT_NAME} !^/web/    
#Then point to web/image.gif
RewriteRule ^(.*)$ web/$1 [L]
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜