php - 如何根据请求的子目录进行不同的 htaccess 重定向?
问题描述
我试着四处寻找一个解决这个问题的问题,但我不知道确切的措辞,所以我找不到任何有用的东西。
我在我的网站上安装了 Wordpress,按照本指南,我能够将其移动到不同的文件夹,因此我的主 public_html 目录中现在有 2 个文件夹。
我尝试将一些 p5.js 草图上传到第二个文件夹,以便我可以从 Wordpress 站点引用它们。当我尝试将它们链接到本指南建议的 iframe 中时,就会出现问题。我引用src="website.com/projects/project_name/sketch.html"
但不幸的是它被重定向回wordpress安装404页面。
经过一些搜索,看起来我的主要 .htaccess 文件是问题所在。我的文件看起来像这样(更改了 example/my_subdir)
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/my_subdir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /my_subdir/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ my_subdir/index.php [L]
</IfModule>
所以我的问题是,我将如何修改它以保持从“website.com”到 my_subdir/index.php(这是 Wordpress 安装)的重定向,但要注意不要重定向 website.com/directory2 请求(这是项目将被存储)
解决方案
如果您的代码中的 HOSTexample.com
是您在问题中所指的内容website.com
,并且我假设您的代码已经正常工作,除了directory2
request. 然后试试这个:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/my_subdir/
RewriteCond %{REQUEST_URI} !^/directory2/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /my_subdir/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ my_subdir/index.php [L]
</IfModule>
已编辑directory2 只需要更改 1 行
下面是我的测试:
IP:172.18.0.2
根目录:/www/htdocs
目录树(html文件内容):
-- htdocs/
| -- my_subdir/
| | -- index.html (subdir index.html)
| | -- a1.html (subdir a1.html)
|
| -- directory2/
| | -- index.html (dir2 index.html)
| | -- b1.html (dir2 b1.html)
|
| -- index.html (helloworld index.html)
| -- .htaccess
下面是我的.htaccess
文件:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(172.18.0.2)$
RewriteCond %{REQUEST_URI} !^(/my_subdir/) [NC]
RewriteCond %{REQUEST_URI} !^(/directory2/) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /my_subdir/$1
RewriteCond %{HTTP_HOST} ^(172.18.0.2)$
RewriteRule ^(/)?$ my_subdir/index.html [L]
我的测试:
- 访问主机
[user@client tmp]$ wget 172.18.0.2
Saving to: ‘index.html’
[user@client tmp]$ cat index.html
subdir index.html
请求已到达 my_subdir/index.html
- 访问 172.18.0.2/my_subdir/a1.html
[user@client tmp]$ wget 172.18.0.2/my_subdir/a1.html
Saving to: ‘a1.html’
[user@client tmp]$ cat a1.html
subdir a1.html
请求已到达 my_subdir/a1.html
- 访问 172.18.0.2/a1.html
[user@client tmp]$ wget 172.18.0.2/a1.html
Saving to: ‘a1.html’
[user@client tmp]$ cat a1.html
subdir a1.html
请求已到达 my_subdir/a1.html
- 访问 172.18.0.2/directory2
[user@client tmp]$ wget 172.18.0.2/directory2
...
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://172.18.0.2/directory2/ [following]
...
Saving to: ‘directory2’
[user@client tmp]$ cat directory2
dir2 index.html
请求已到达 directory2/index.html
- 访问 172.18.0.2/directory2/b1.html
[user@client tmp]$ wget 172.18.0.2/directory2/b1.html
Saving to: ‘b1.html’
[user@client tmp]$ cat b1.html
dir2 b1.html
请求已到达 directory2/b1.html
- 访问 172.18.0.2/testerror
[user@client tmp]$ wget 172.18.0.2/testerror
ERROR 404: Not Found.
[user@server log]$ tail error_log
AH00128: File does not exist: /www/htdocs/my_subdir/testerror
请求试图到达 my_subdir/testerror,但什么也没找到
所以基于这个小测试:
- 对我的 IP 主机的任何请求都已检查
.htaccess
重写规则 - 仅请求主机将被重写为 my_subdir/index.html(测试 1)
- 不以任何后续字符串开头
my_subdir
或directory2
将全部重写的请求子 URL(测试 3 和 6)my_subdir
my_subdir
带有详细文件名的请求将访问my_subdir/
(测试2)下的文件- 请求
directory2
不会被重定向到,并且会访问(测试 4 和 5)my_subdir
下的索引或文件directory2
推荐阅读
- python - 使用 Python 从星系照片中删除背景
- javascript - 使特定的表格可滚动?
- c - 由于 unsigned int 变量类型在 c 中有无限循环时会发生什么
- python - 在Python中将垂直系列重塑为水平系列
- javascript - 引用静态方法内部的类而不使用其名称
- javascript - 将变量从 javascript 传递给 python,无需重新加载页面
- javascript - Non-greedy capturing parenthesis
- python-3.x - 如何为 CIFAR 10 pytorch 教程添加 GPU 计算?
- amazon-web-services - 如何通过节点红色 MQTT 设置 AWS SNS 电子邮件的电子邮件主题?
- swift - “找不到适用于应用程序的 WatchKit 1.0 应用程序包”Xcode 10.1 Swift 4.2