首页 > 解决方案 > 在 url 中添加操作后,没有获取 .css 和 .js 文件

问题描述

我正在申请。如果我的请求带有 url:http://localhost/invoice/public_html/index.php/string-inventory 一切正常。

如果我使用 url 请求:http://localhost/invoice/public_html/index.php/string-inventory/new-recordhttp://localhost/invoice/public_html/index.php/string-inventory/

我的应用程序没有正确的 .css 或 Js 服务器。我解决了从 index.php(没有 css 和 js 文件)到 index.php/(带有 css 和 js 文件)的此类问题,同时重定向到整个不同的页面。但这不是正确的做法。对于许多具有相同上下文的页面,我得到 404 的 .css 和 .js 文件。

如果我评论

RewriteCond %{REQUEST_URI} !。.css$ [NC] RewriteCond %{REQUEST_URI} !. .js$ [NC]

然后是 404 但文件没有被提供

我的 .htaccess

开启重写

Options +FollowSymLinks
RewriteEngine On

# Redirect requests to index.php

RewriteCond %{REQUEST_URI} !=/index.php
RewriteCond %{REQUEST_URI} !.*\.png$ [NC]
RewriteCond %{REQUEST_URI} !.*\.jpg$ [NC]
RewriteCond %{REQUEST_URI} !.*\.css$ [NC]
RewriteCond %{REQUEST_URI} !.*\.gif$ [NC]
RewriteCond %{REQUEST_URI} !.*\.js$ [NC]

#RewriteCond $1 !^(../src)

 RewriteRule .* index.php?page=$1 [NC,L,QSA]

我预期的 .css 网址应该是 http://localhost/invoice/src/css/mystyles.css

它在哪里 http://localhost/invoice/public_html/src/css/mystyles.css

但如果控制器后没有动作,它工作正常。我确实得到了预期的文件。Js也一样。

有什么需要就问

标签: php.htaccessurl-rewriting

解决方案


我无法查看您的文件结构,但您的 /invoice 似乎是您的项目 url,而 public_html 上的文件夹 1 实际上是您的公共根文件夹。

我可以建议您为您的项目调整 apache 设置(通常是 /etc/apache2/sites-available),使 public_html 文件夹是实际的公用文件夹。这将解决上述问题。

.htaccess 文件的另一个提示。如果请求的 url 实际上不是现有文件 (!-f) 或目录 (!-d),您可以在重写之前检查,而不必在列表中定义每个可能的文件类型。

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ index.php?rt=$1 [NC,L,QSA] (or whatever rewrite you want to do)

推荐阅读