如何安排一个wordpress htaccess来访问一个里面有完整网站的目录

How to arrange a wordpress htaccess to access a directory with a complete web inside

我无法编辑 .htaccess 文件。 我在 php slim 框架中创建了一个完整的网站(我们称之为网站 S)。 我的客户有一个 wordpress 网站(我们称之为网站 W),该网站将链接到我的网站 S。 我把我网站S的所有文件复制到网站W的根目录下的一个目录下。

我的想法是更改 follow htaccess(由 wordpress 创建)以允许 public 访问该目录而不会造成任何问题以进入网站 S。

首先.htaccess

RewriteEngine On
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteCond %{QUERY_STRING} !wc-api [NC]
RewriteCond %{HTTP_HOST} ^website.pe$ [OR]
RewriteCond %{HTTP_HOST} ^www.website.pe
RewriteRule ^(.*)$ https://website.pe/ [R=301,L,NE]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

然后,我想我可以在网站 S 所在的目录中使用适用于它的典型 htaccess。 已编辑 我添加了网站 S 的结构。index.php 位于 public 目录中。

第二个.htaccess

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{REQUEST_URI}:: ^(/.+)/(.*)::$
  RewriteRule ^(.*) - [E=BASE:%1]
  
  RewriteBase /
  #RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^ index.php [QSA,L]
</IfModule>

我尝试了不同的方式,但是,还是收到了消息 “禁止访问您没有访问此资源的权限。” 或进入但网站 W 停止工作。

有什么建议吗?提前致谢。

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{REQUEST_URI}:: ^(/.+)/(.*)::$
  RewriteRule ^(.*) - [E=BASE:%1]
  
  RewriteBase /
  #RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^ index.php [QSA,L]
</IfModule>

在您发布的配置中,您只需要从“slim”子目录中的第二个 .htaccess 文件中删除 RewriteBase / 指令。这会导致对您的 slim 网站的请求通过文档根目录中的 WordPress 站点返回(这可能会导致 404)。

My idea is to change the follow htaccess (created by wordpress) to allow public access to the directory without creating any problem to move inside website S

您不需要触摸文档根目录中的 WordPress .htaccess 文件。这已经允许您访问网站 S.

默认情况下,slim 子目录中的 .htaccess 文件(或者更确切地说,mod_rewrite 指令)将完全覆盖根目录中的 WordPress .htaccess 文件。

因此,您需要在 slim 子目录中重复 HTTP 到 HTTPS 重定向(有区别*1),在现有指令之前。据推测,其目的是也将 www 重定向到非 www?尽管您当前的重定向(在根 .htaccess 文件中)没有正确执行此操作。

例如,在 slim 目录中的第二个 .htaccess 文件的顶部添加以下内容:

# Redirect HTTP to HTTPS and www to non-www
RewriteCond %{ENV:HTTPS} !on [NC,OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(website\.pe) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

*1 注意使用 REQUEST_URI 变量而不是 </code> 反向引用根 <code>.htaccess 文件。这在子目录中使用时是必须的,否则子目录将被重定向 URL.

注意:首先使用 302(临时)重定向进行测试以避免缓存问题。


更新:

But I can't make the website S to run because inside the website S directory exist a public subdirectory with the index.php, arranged like this:

website.pe/ (website W wordpress)
-slimdirectory/ (website S slim)
--vendor/
--public/
---css/
---fonts/
---images/
---js/
---index.php
--bootstrap/
---app.php
--etc..

这是您最初的问题中缺少的相当重要的信息。我假设 public 子目录是可见 URL 的 而不是 的一部分,在这种情况下,您的第二个 .htaccess 文件位于 slim 子目录(父目录)中public) 的目录应该是这样的:

# /slimdirectory/.htaccess

RewriteEngine On

# Redirect HTTP to HTTPS and www to non-www
RewriteCond %{ENV:HTTPS} !on [NC,OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(website\.pe) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

# Calculate base directory
RewriteCond %{REQUEST_URI}:: ^(/.+)/(.*)::$
RewriteRule (.*) - [E=BASE:%1]

# Slim front-controller
RewriteRule ^public/index\.php - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . public/index.php [L]
RewriteRule ^$ public/index.php [L]

不需要 QSA 标志。

您不需要 <IfModule> 包装器。

是的,White 先生,我在第二个 .htaccess 中删除了 RewriteBase /,现在我可以在我用于测试的 slim 网站所在的目录中访问一个简单的 index.html。

但是我无法将网站S设为运行,因为在网站S目录中存在一个public子目录index.php,排列如下:

website.pe/ (website W wordpress)
-slimdirectory/ (website S slim)
--.htacess
--vendor/
--public/
---css/
---fonts/
---images/
---js/
---index.php
--bootstrap/
---app.php
--etc..

我是 htaccess 的新手,不明白一个 htaccess 如何覆盖另一个,所以如果我在我的第二个 htaccess 中使用你告诉我的东西,就像这样

<IfModule mod_rewrite.c>
   # Redirect HTTP to HTTPS and www to non-www
   RewriteCond %{ENV:HTTPS} !on [NC,OR]
   RewriteCond %{HTTP_HOST} ^www\. [NC]
   RewriteCond %{HTTP_HOST} ^(?:www\.)?(website\.pe) [NC]
   RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

   RewriteEngine on
   RewriteCond %{REQUEST_URI} !^public
   RewriteRule ^(.*)$ public/ [L]

   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteRule ^ index.php [QSA,L]
</IfModule>

那应该可以...但还是不行