【发布时间】:2021-10-12 22:41:42
【问题描述】:
我的 .htaccess 文件有问题。它与 index.php(我的前端控制器)以及有关 CSS、JavaScript 和图像的文件夹一起放置在我网站的根文件夹中。以下是我的.htaccess文件的URL相关内容
# skip existent files
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule index.php - [QSA,L,C]
RewriteRule .* - [QSA,L]
# protect PHP files from the outside
RewriteCond %{REQUEST_URI} ^.*\.php$
RewriteRule ^.*\.php$ - [R=404,L]
RewriteCond %{REQUEST_URI} ^img/*$
RewriteRule ^img/*$ - [QSA,L]
# refer root to index.php
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^$ index.php [QSA,L]
# redirect 404 for non existent files
RewriteCond %{REQUEST_URI} ^(.*)\..*$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\..*$ - [R=404,L]
# adjust the rest of the domains
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?site=$1 [QSA,L]
当我在浏览器中打开网站时,它不会加载相应的资源。如果我点击“显示图片源”,它立即指向404页面。
您知道我需要调整哪个部分来解决这个问题吗?根结构如下所示
root
-.htaccess
-/css
-/js
-/img
-index.php
提前非常感谢您! :)
编辑 2021 年 10 月 8 日: 我发现问题源于我在 RewriteRule ^(.*)$ index.php?site=$1 [QSA,L] 之前放入最后一部分的其他 RewriteRules,例如:
RewriteRule ^study-programmes/(.*)$ index.php?site=study-programmes&faculty=$1 [L]
但是,我有几个页面需要专门命名的参数(例如上面示例中的教师)。我究竟需要调整什么才能使其正常工作?
【问题讨论】:
-
尝试删除所有内容并将每个重写块添加回调试。另一个想法是将不存在的文件块放在开头,以便首先执行。
-
干杯!我这样做并发现似乎我没有在此处发布的其他重写规则(例如 RewriteRule ^study-programmes/(.*)$ index.php?site=study-programmes&faculty=$1 [L])已被阻止无论出于何种原因的资源...我将它们放在 RewriteRule ^(.*)$ index.php?site=$1 [QSA,L] 的最后一个块之前
-
您在 HTML 代码中究竟是如何引用这些资源的?我的怀疑是,当您在主文档 URL 中引入新的“文件夹级别”时,您可能没有考虑到相对 URL 的基本路径如何针对更改进行解析。
-
我仅指 index.php,其中参数“site”用于定义页面本身(例如联系人、印记等)。几个页面(例如学习计划,因此 index.php?site=study-programmes)需要额外的参数才能“更深一层”,即使我不是指实际的文件夹。前端控制器只是根据是否给出附加参数显示不同的内容
标签: .htaccess url model-view-controller mod-rewrite front-controller