大网数据广告活动
服务器运维问答

Recommended htaccess breaks all static content

· 52CE 我爱测

I am struggling to get my website's CSS, IMG and JS resources to show. My website has this structure: root classes templates public images scripts styles index.php Procfile apache.conf Now, I know this problem is caused by my apa…

问题原文

原帖链接 · Jimmy · CC BY-SA 4.0

I am struggling to get my website's CSS, IMG and JS resources to show. My website has this structure:

root
   classes
   templates
   public
    images
    scripts
    styles
    index.php
   Procfile
   apache.conf

Now, I know this problem is caused by my apache rewrites because when I include them, my PHP works but my images don't. When I don't include them, my PHP is broken by the assets work. Strangely thought I had to add a slash to the front of my rewrite rule, as the originally recommended index.php did not work and had to be changed to /index.php

My Proc file sets up public to be the root, and my rewrites look like this:

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

With the above, my PHP code works fine. My question is, how please can I get it so the rewrite results don't break all my assets from loading? When I view the source of the site, they are located in the right place, just I think they are being blocked from loading.

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

The apache.conf has the apache rewrites written above

apache.conf would seem to be your "server config". When mod_rewrite directives are used in a server context, as opposed to a directory (.htaccess) context they work differently and can require a different syntax. This explains why you needed to prefix the substitution string with a slash in order to make it relative to the DocumentRoot (without the slash it is seen as relative to the server root).

Consequently, the REQUEST_FILENAME server variable cannot be used as-is in a server context, since the request has not yet been mapped to the filesystem. REQUEST_FILENAME will be the same as REQUEST_URI and your filesystem checks will fail (resulting in your static resources being rewritten).

To get the final value of the REQUEST_FILENAME variable in a server context, you need to use a URL-based look-ahead, ie. %{LA-U:REQUEST_FILENAME}.

Try the following instead:

RewriteEngine On
RewriteCond %{LA-U:REQUEST_FILENAME} !-s
RewriteCond %{LA-U:REQUEST_FILENAME} !-l
RewriteCond %{LA-U:REQUEST_FILENAME} !-d
RewriteRule ^ /index.php [L]

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-01-07)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。