php - .htaccess 阻止 CSS、javascript 和图像从子目录获取访问权限
问题描述
我搜索这个,但没有一个解决方案适合我。我已经从这里尝试了许多不同的“解决方案”,但都没有奏效。
我在我的域的主根目录中安装了 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
.htaccess for PHP 脚本位于 example.com/app/index.php 的子目录:
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 访问
解决方案
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 路径不正确造成的。如果您使用表单的根相对 URL 引用您的 CSS,/css/style.css
那么这将不起作用。您需要使 URL 路径相对(即删除斜杠前缀,如 中的 mod_rewrite 指令.htaccess
),或在链接中包含完整的 URL 路径(首选)。(这与@starkeen 在评论中引用的相反。)
另请参阅网站管理员 statck 上的相关问题:
推荐阅读
- java - 如何在 Spring 数据 r2dbc 中替换 @PrePersist
- javascript - Jquery从另一个链接设置href
- r - 通过 rvest 提取内容的 R web 抓取问题
- php - 如何在使用 Laravel 7 的视图中根据文件类型显示图标?
- vue.js - 如何将目录中的所有“vue”文件包含到构建中而不显式导入它们
- docker - vuejs dev 为 docker 构建动态输出(每次更改后更新文件)
- javascript - 仅当用户单击取消时如何触发 CancellationToken
- javascript - 我应该如何创建这个
- php - 如何从自定义异常消息中删除默认的 Laravel 异常消息
- django - 带有 WSGI ModuleNotFoundError 的 Apache 日志