.htaccess 根据 HTTPS 下的路由加载不同的 Web 应用程序
我正在开发一个项目,该项目由一名软件工程师和一群懂得如何使用 Wordpress 的非技术人员共同完成。这意味着平台的一部分是手工制作的,但仍然需要有Wordpress内容由非软件工程师处理。 我的想法是在我的网络服务器根目录中有两个文件夹,一个名为 /app/ 包含手工代码,另一个名为 /wp/ 。因此,当 GitHub 管道将新代码发布到 /app/ 时,一定不会触及 /wp/ 中包含 Wordpress 的内容。 我已经使用以下代码实现了强制 HTTPS:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
它就像一个魅力,如果用户使用 HTTP,他们就会成功重定向。 现在的问题是,我想为自己“保留”手工平台使用的路线,并且万一用户没有调用任何这些路线,那么我将球传给Wordpress。我希望它显示为单个网站,所以我不希望用户加载 /app/appRoute 或 /wp/wpRoute,我希望始终加载 /route1、/route2,而不指定子文件夹到 URL 中。 手工平台应该优先,它使用大约 13 条主路由(其中一些还有子路由),所以我猜我可以将它们硬编码到 .htaccess 文件中。如果用户尝试加载任何这些路由,那么我想将内容加载到 /app/route 中,如果没有,我会加载 /wp/route。当然,Wordpress 有自己的 .htaccess 文件,平台也有自己的,即:
RewriteEngine on
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
我非常不擅长处理 .htaccess 文件并且一直在四处搜索,看起来我找不到适合我的场景的解决方案。 我应该在网络服务器根目录中有一个 .htaccess 文件来删除 Wordpress 文件吗? 我应该有两个 .htaccess,先写 13 条路由,然后最终重定向到 WP .htaccess (这可能吗?)? 我是否有让用户面临“重定向过多”错误的风险?
这种混合解决方案让我很困惑。有没有遇到过同样情况的人给点建议?先感谢您。
I'm working on a project made by a software engineer and by a team of non-technical people who know how to use Wordpress. That means that part of the platform is handmade, but still need to have Wordpress contents to be handled by non-software engineers.
My idea is to have two folders in my webserver root, one called /app/ containing the handmade code, and one called /wp/. So when the GitHub pipeline release new code into /app/ is sure not to touch stuff in /wp/ containing Wordpress.
I have achieved forcing the HTTPS with the following code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
It works like a charm, users are successfully redirected if they use HTTP.
Now the problem is, I want to "reserve" the routes used by the handmade platform for myself, and in case the user is not calling any of those routes, then I pass the ball to Wordpress. I want it to appear as it is a single website, so I don't want the user to load the /app/appRoute or the /wp/wpRoute, I'd like to always load /route1, /route2, without specifying the subfolder into the URL.
The handmade platform should have priority, and it uses around 13 main routes (and some of them have subroutes), so I can hardcode them into the .htaccess file I guess. If the user is trying to load any of those routes, then I want to load the content into /app/route, if not, I'd load /wp/route. Of course Wordpress has its own .htaccess file, and the platform has its own, which is:
RewriteEngine on
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
I'm very bad at working on .htaccess files and been searching around, it looks like I can't find the solution for my scenario.
Should I have a single .htaccess file in the webserver root deleting the Wordpress one?
Should I have two .htaccess, write the 13 routes first, and then eventually redirect to the WP .htaccess (is it even possible?)?
Do I risk to make the user face a "Too many redirects" error?
This hybrid solution confuses me a lot. Does anyone who has been in the same situation has suggestions? Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
给定以下要求:
/app
子目录包含“手工代码”/wp
子目录包含 WordPress 站点。/app
或/wp
均不应出现在可见 URL 中。你可以,但我不会。将 WordPress
.htaccess
文件保留在/wp
子目录中。 WordPress 的所有内容都位于/wp
子目录中。我将使用 3 个
.htaccess
文件:其中一个位于文档根目录中。它管理到
/app
或/wp
(WordPress) 中“手工代码”的路由。这还应该管理规范重定向(即 HTTP 到 HTTPS 和 www 与非 www)/app
子目录中的一个,用于管理您的“手工代码”。/wp
子目录中的一个用于管理 WordPress 内的路由。这允许您将“手工代码”和 WordPress 完全分开(在开发方面)。
您的 3 个
.htaccess
文件将如下所示:/.htaccess
如果存在以下情况,则可以合并“对
/app
的特定重写”图案。有关静态资产,请参阅下文。/app/.htaccess
/wp/.htaccess
请注意
RewriteBase
指令和替换字符串上的斜杠前缀是特别排除,以避免必须在.htaccess
文件本身中指定/app
或/wp
。 (虽然这可能会扰乱 WordPress,但它喜欢(不必要地)使用RewriteBase
并尝试覆盖 WP 代码块。)您不需要重复
RewriteEngine
指令,这已经在 WP 代码块中稍后出现。我不知道你想如何处理你的静态资产/资源(CSS、JS、图像等)?目前,上面假设您将直接链接到
/app
中的资产,即。通过在资产链接中包含/app
路径段。例如。
。使用 WordPress,您可以直接链接(即包含/wp
前缀)或省略/wp
,因为其他所有内容都会被重写为/wp
。理想情况下,最好从资产链接中省略
/app
和/wp
,因为您不希望将这些不必要地暴露给用户,而且会否则使站点依赖于这些父目录。如果您的“手工代码”对所有资源使用
/assets
,那么您可以在自定义路由重写之前,在根目录中的父.htaccess
文件中重写这些内容:这允许您的“手工代码”使用根相对 URL 引用资产,就像应用程序安装在文档根目录中一样。
Given the following requirements:
/app
subdirectory contains the "handmade code"/wp
subdirectory contains the WordPress site./app
or/wp
should appear in the visible URL.You could, but I wouldn't. Keep the WordPress
.htaccess
file in the/wp
subdirectory. Everything WordPress is in the/wp
subdirectory.I would use 3
.htaccess
files:One in the document root. This manages the routing to either the "handmade code" in
/app
or/wp
(WordPress). This should also manage the canonical redirects (ie. HTTP to HTTPS and www vs non-www)One in the
/app
subdirectory that manages the routing within your "handmade code".One in the
/wp
subdirectory that manages the routing within WordPress.This allows you to keep the "handmade code" and WordPress entirely separate (in terms of development).
Your 3
.htaccess
files would then look like this:/.htaccess
The "specific rewrites to
/app
" can be combined if there is a pattern. See below regarding static assets./app/.htaccess
/wp/.htaccess
Note the
RewriteBase
directives and slash prefix on the substitution strings are specifically excluded, to avoid having to specify/app
or/wp
in the.htaccess
file itself. (Although this might mess with WordPress, that likes to (unnecessarily) useRewriteBase
and will try to overwrite the WP code block.)You do not need to repeat the
RewriteEngine
directive, that already occurs later in the WP code block.I don't know how you want to handle your static assets/resources (CSS, JS, images, etc.)? Currently, the above assumes that you will link directly to the assets within
/app
, ie. By including the/app
path segment in the asset link. eg.<image src="/app/assets/images/myimage.png">
. With WordPress you could link directly (ie. include/wp
prefix) or omit/wp
, since everything else is rewritten to/wp
anyway.Ideally, it would probably be preferable to omit both
/app
and/wp
from your asset links, since you don't want to unnecessarily expose these to your users and it would otherwise make the sites dependent on these parent directories.If your "handmade code" uses
/assets
for all the assets then you can rewrite these in the parent.htaccess
file in the root, before your custom route rewrites:This allows your "handmade code" to refer to assets using root-relative URLs, as if the app was installed in the document root.