从子目录获取 Zend 布局链接正常工作的 Zend 应用程序

2021-12-29 00:00:00 php zend-framework .htaccess

我在子目录中设置了 Zend 应用程序.不要问为什么我必须这样做(不是我喜欢的方法),但我没有那个选择.Zend 应用程序驻留在名为 dsa 的子目录中.

http://dsa.dev/dsa

我的.htaccess

RewriteEngine OnRewriteCond %{REQUEST_FILENAME} -s [OR]RewriteCond %{REQUEST_FILENAME} -l [OR]RewriteCond %{REQUEST_FILENAME} -d重写规则 ^(.*)$ public/index.php [QSA,L]

这有效并将引导应用程序.我可以去我的根目录和其他目录做其他事情,没有任何问题.

在我的 application.ini 中我已经设置了

resources.frontController.baseUrl = "/dsa".

在我的 layout.phtml 文件中,如果我 var_dump 路径看起来像这样.

var_dump($this->baseUrl('css/main.css'));http://dsa.dev/dsa/css/main.css

所有链接都不起作用,所有 css 和 js 文件都没有正确链接.如果你按 F12 并查看它们,你会得到一个 404 未找到任何文件.我需要做什么才能获得正确的路径来显示文件.这是我第一次尝试在子目录中设置 zend,显然我没有做正确的事情.我已经阅读了我能找到的关于这个主题的所有内容.感谢您的帮助.

解决方案

我看到的至少一个问题是 .htaccess 文件中的重写规则不正确.

鉴于这些规则:

RewriteEngine OnRewriteCond %{REQUEST_FILENAME} -s [OR]RewriteCond %{REQUEST_FILENAME} -l [OR]RewriteCond %{REQUEST_FILENAME} -d重写规则 ^(.*)$ public/index.php [QSA,L]

这些规则等同于:

RewriteCond %{REQUEST_FILENAME} -s [OR] # 请求是一个大小 > 0 的常规文件RewriteCond %{REQUEST_FILENAME} -l [OR] # 请求是一个符号链接文件RewriteCond %{REQUEST_FILENAME} -d [OR] # 请求到一个存在的目录

如果上述任何一项为真,请求将被重写为不正确的 public/index.php.如果请求是针对磁盘上存在的文件,则不要重写它;相反,您只想提供该文件.

正确的 .htaccess 文件应该如下所示:

RewriteCond %{REQUEST_FILENAME} -s [OR]RewriteCond %{REQUEST_FILENAME} -l [OR]RewriteCond %{REQUEST_FILENAME} -d重写规则 ^.*$ - [NC,L]重写规则 ^.*$ public/index.php [NC,L]

这表示,如果请求是针对大小 > 0 的文件、目录或符号链接,则将请求路由到所请求的实际文件 (RewriteRule ^.*$ -).如果这些条件都不成立,则将请求重写到 index.php.

另外说明一下,我会继续彻底删除 public 目录.把public的内容放到/dsa中.dsa 现在是您的 public 文件夹.您可以将 application 目录存储在系统的任何位置,或者如果由于文件系统限制而必须将其放置在 dsa 文件夹中,请确保将规则添加到您的 .htaccess 文件拒绝对 application 文件夹的所有访问.然后您只需要快速更改您的 index.php 文件,使用 APPLICATION_PATH 常量告诉它application 的正确路径.>

我相信这两个更改应该可以解决您的问题.

I have setup a Zend application in a sub directory. Dont ask why I just had to do it that way (not my preferred method) but I don't have that choice. The Zend application resides in a subdirectory called dsa.

http://dsa.dev/dsa

My .htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ public/index.php [QSA,L] 

This works and will bootstrap the application. I can go to my root directories and other directories for other things without any problems.

In my application.ini I have set

resources.frontController.baseUrl = "/dsa".  

In my layout.phtml file if I var_dump the path it looks like this.

var_dump($this->baseUrl('css/main.css'));
http://dsa.dev/dsa/css/main.css

None of the links work, all css and js files are not linking correctly. If you hit F12 and look at them you get a 404 not found for any file. What do I need to do to get the correct paths to show the files. This is the first time I have tried setting zend up in a sub directory and obviously I'm not doing something right. I have read everything I can find on this subject. Thanks for the help.

解决方案

At least one problem I see is that the rewrite rules in the .htaccess file are incorrect.

Given these rules:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ public/index.php [QSA,L]

Those rules equate to:

RewriteCond %{REQUEST_FILENAME} -s [OR] # The request is a regular file with size > 0 RewriteCond %{REQUEST_FILENAME} -l [OR] # The request is to a file that is a symlink RewriteCond %{REQUEST_FILENAME} -d [OR] # The request is to a directory that exists

And if any of the above are true, the request is rewritten to public/index.php which is not correct. If the request is for a file that exists on disk, you DO NOT want to rewrite it; instead you just want to serve that file.

The correct .htaccess file should look something like this:

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ public/index.php [NC,L]

This says, if the request is for a file with size > 0, a directory, or a symlink, then route the request to the actual file requested (RewriteRule ^.*$ -). If none of those conditions are true, then rewrite the request to index.php.

On a separate note, I would go ahead and get rid of the public directory altogether. Take the contents of public and put them in /dsa. dsa is now your public folder. You can store your application directory anywhere on the system, or if you have to place it in the dsa folder due to filesystem restrictions, make sure to add a rule to your .htaccess file that denies all access to the application folder. Then you just need to make a quick change to your index.php file telling it the correct path to application using the APPLICATION_PATH constant.

I believe those two changes should fix your problems.

相关文章