【问题标题】:.htaccess are blocking CSS, javascript, and images from getting access from sub-directory.htaccess 阻止 CSS、javascript 和图像从子目录获取访问权限
【发布时间】:2019-05-21 20:18:41
【问题描述】:

我搜索了这个,但没有一个解决方案适合我。 我已经从这里尝试了许多不同的“解决方案”,但都没有奏效。

我在我的域的主根目录中安装了 WordPress,例如 example.com/index.php
所以我在 example.com 的根目录中有一个 WordPress 的 .htaccess:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on [NC]
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

之后,我将其他 PHP 脚本安装在主域的子目录中,例如 example.com/app/index.php

example.com/app/index.php 子目录中 PHP 脚本的 .htaccess :

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

RewriteEngine On
RewriteBase /
# RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
# RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# RewriteCond %{HTTPS} off
# RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^search/(.+)$ /search.php?q=$1
RewriteRule ^search/([^/]*?)/page/([^/]*?)/$ /search.php?q=$1&token=$2
RewriteRule ^search/([^/]*?)/$ /search.php?q=$1
RewriteRule ^download/([^/]*?)/page/([^/]*?)/$ /search.php?q=$1&token=$2
RewriteRule ^download/([^/]*?)/$ /search.php?q=$1
RewriteRule ^watch/(.+)$ /watch.php?id=$1  [L]
RewriteRule ^home/(.+)$ /index.php?q=&page=$1 [L]
RewriteRule ^(cache/|submit\.php|head\.php|head_mobile\.php|foot\.php|foot_mobile\.php|includes\.php|config\.php) - [F,L,NC]
RewriteRule ^sitemap.xml$ /sitemap.php [L]
RewriteRule ^dmca$ /dmca.php
RewriteRule ^privacy$ /privacy.php
RewriteRule ^contact$ /contact.php

现在我的 PHP 脚本无法正常工作,并且在安装 PHP 脚本的子目录中无法访问所有文件,包括 CSS、JS、图像。

我希望 URL 应该是这样的:

https://example.com/app/watch/wVS-hHZsAIg
https://example.com/app/contact-us
https://example.com/app/dmca
https://example.com/app/css/style.css

但是上述所有 URL 都不起作用,并且它重定向到 WordPress 安装页面并给出如下 404 错误:

https://example.com/watch/wVS-hHZsAIg
https://example.com/contact-us
https://example.com/dmca
https://example.com/css/style.css

我的 CSS、JS 和其他相关资源无法正常工作。这个问题。

而且 .htaccess 会阻止 CSS、javascript 和图像通过 example.com/app/index.php 访问

【问题讨论】:

标签: php .htaccess directory


【解决方案1】:
RewriteRule ^watch/(.+)$ /watch.php?id=$1  [L]

您的所有重写(例如上面的重写)都将 URL 重写到安装 WordPress 的文档根目录。因此,/app/watch/wVS-hHZsAIg 的请求被重写为 /watch.php?id=wVS-hHZsAIg(通过上述指令),我假设它不存在,因此它将通过 WordPress 路由,导致 404。

要让这个.htaccess 文件在/app 子目录中工作,您需要从所有替换 字符串中删除斜杠前缀。例如,上面的指令应该是:

RewriteRule ^watch/(.+) watch.php?id=$1 [L]

RewriteRule 上的尾随 $ 模式 是多余的。)

https://example.com/app/contact-us

您发布的指令寻找的是contact,而不是contact-us - 所以这可能与上述无关。

https://example.com/app/css/style.css

同样,这与上述无关。并且可能是由于您的 HTML 中的 URL 路径不正确造成的。如果您使用 /css/style.css 形式的根相对 URL 引用您的 CSS,那么这将不起作用。您需要使 URL 路径相对(即删除斜杠前缀,如 .htaccess 中的 mod_rewrite 指令),或在链接中包含完整的 URL 路径(首选)。 (这与 @starkeen 在 cmets 中引用的相反。)

另请参阅 Webmasters statck 上的相关问题:

【讨论】:

    猜你喜欢
    • 2023-04-01
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 2014-03-31
    • 2013-04-22
    • 1970-01-01
    相关资源
    最近更新 更多