首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何根据请求的子目录进行不同的htaccess重定向?

如何根据请求的子目录进行不同的htaccess重定向?
EN

Stack Overflow用户
提问于 2020-02-27 09:33:17
回答 1查看 47关注 0票数 0

我试着四处寻找一个解决这个问题的问题,但我不知道确切的措辞,所以我找不到任何有用的东西。

我在我的网站上安装了一个Wordpress,遵循this指南,我可以把它移到一个不同的文件夹,所以我现在在我的主public_html目录下有两个文件夹。

我正在尝试将一些p5.js草图上传到第二个文件夹,这样我就可以从Wordpress站点引用它们。当我尝试像this guide建议的那样在iframe中链接它们时,问题就出现了。我引用了src="website.com/projects/project_name/sketch.html",但不幸的是,它被重定向回wordpress installs404页面。

经过一些搜索,看起来我的主.htaccess文件才是问题所在。我的文件如下所示(示例/ My _subdir已更改)

代码语言:javascript
运行
复制
<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“到modify /index.php(这是Wordpress的安装)的重定向,但添加一个警告,不重定向website.com/directory2请求(项目将被存储的地方)

EN

回答 1

Stack Overflow用户

发布于 2020-02-27 11:17:46

如果您的代码example.com中的主机是您在问题中提到的website.com,并且我假设您的代码已经正常工作,除了directory2请求。然后试试这个:

代码语言:javascript
运行
复制
<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只需要更改一行

下面是我的测试:

IP: 172.18.0.2

根目录: /www/htdocs

目录树(html文件内容):

代码语言:javascript
运行
复制
-- 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文件:

代码语言:javascript
运行
复制
 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]

我的测试:

  1. access主机

代码语言:javascript
运行
复制
 [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

  1. access 172.18.0.2/my_subdir/a1.html

代码语言:javascript
运行
复制
 [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

  1. access 172.18.0.2/a1.html

代码语言:javascript
运行
复制
 [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

  1. access 172.18.0.2/directory2

代码语言:javascript
运行
复制
 [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

  1. access 172.18.0.2/directory2/b1.html

代码语言:javascript
运行
复制
 [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

  1. access 172.18.0.2/testerror

代码语言:javascript
运行
复制
 [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,但未找到任何内容

基于这个小测试的So:

  1. 发送到我的IP主机的任何请求都已选中重写.htaccess仅主机将重写到my_subdir/index.html (测试1)不是以my_subdirdirectory2开头的
  2. 请求子url将全部重写为带有任何后续字符串的my_subdir (测试3和6)

H166请求(带有详细文件名)对D67的请求将访问D68下的文件(测试2)H269对D71的请求不会重定向到D72,并将在directory2 (测试4和5)下访问索引或文件

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60425088

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档