htaccess redirect to public and application folder

北城余情 提交于 2020-01-07 08:09:09

问题


I have this directory structure for a MVC project:

/
|   .htaccess
|   index.php
+---application
|   +---app
|   |   +---controller
|   |   |       AppController.class.php
|   |   +---files
|   |   |       style.css
|   |   +---model
|   |   \---views
|   |           AllApps.php
|   |           Main.php
|   \---login
|       +---controller
|       |       LoginController.class.php
|       +---files
|       |       application.js
|       |       login.css
|       |       background.png
|       +---model
|       |       LoginModel.class.php
|       \---views
|               Login.php
\---public
    |   .htaccess
    |   index.php
    +---css
    |       bootstrap.css
    +---js
    |       bootstrap.js
    |       jquery.js

The htacces file in the root directory contains this:

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

Meanwhile htaccess file in the public folder contains:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond $1 !^(index\.php|resources|robots\.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)$  index.php?url=$1&%{QUERY_STRING} [PT,L]   
 </IfModule>

What I need is to access the files in each application folder when access is from the following url: mysite.com/__files/login/login.css

I was trying this in the htaccess of the root folder but doesn't work.

RewriteRule ^__files/([\d\w]+)/([\d\w]+\.[A-z]{1,4})$    application/$1/files/$2    [L]
RewriteRule  ^((?!__files).)*$  public/$0 [L]

Where $1 is the app folder name and $2 is the file name

For testing purpose in index.php file print the $_GET array and always print

Array
(
    [url] => application/login/files/application.js
)

The strange thing is when I comment the line

RewriteRule  ^((?!__files).)*$  public/$0 [L]

the access to files in the "files" is correct.

I think after the redirection to file files folder is folder redirection to the public folder, but it shouldn't be.


回答1:


Here is the code that works for myself

RewriteEngine on
RewriteCond $1 !^(__files|application) [NC]
RewriteRule  (.*)  public/$1  [L]

RewriteCond $1  ^(__files)*  [NC]
RewriteRule ^__files/([^/]+)/([^/]+)/? application/$1/files/$2 [L,NC]

The important line is this

RewriteCond $1 !(__files|application) [NC]

I had tu add application with *__files*, it seems that after doing redirect to the application directory it's redirecting to the public folder too.



来源:https://stackoverflow.com/questions/16243096/htaccess-redirect-to-public-and-application-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!