htaccess重定向到公用和应用程序文件夹 [英] htaccess redirect to public and application folder

查看:72
本文介绍了htaccess重定向到公用和应用程序文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于MVC项目,我具有以下目录结构:

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

根目录中的htacces文件包含以下内容:

The htacces file in the root directory contains this:

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

同时公用文件夹中的htaccess文件包含:

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>

当需要从以下URL访问时,我需要访问每个应用程序文件夹中的文件:mysite .com / __ files / login / login.css

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

我在根文件夹的htaccess中尝试了此操作,但没有用。

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]

其中$ 1是应用程序文件夹名称,$ 2是文件名

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

用于索引中的测试。 php文件打印$ _GET数组,并始终打印

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

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

奇怪的是,当我评论时

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.

推荐答案

以下是该代码为自己工作

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]

重要的一行是


RewriteCond $ 1!(__ files | application)[NC]

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

我已经在 * __ files * 中添加了 application ,看来,在重定向到应用程序目录后,它也重定向到了公用文件夹

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.

这篇关于htaccess重定向到公用和应用程序文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆