Apache Rewrite:用于域特定 RedirectMatch 的辅助 htaccess

发布于 2025-01-07 11:23:28 字数 946 浏览 1 评论 0原文

在共享网络托管上,我的软件支持多个域(所有域都指向同一个 public_html 根目录)。

我想要做的是将重定向(以及任何 RedirectMatch)保留在其自己的主机特定/专用 .htaccess 文件中。

从视觉上看,目录结构看起来像这样......

/public_html/(所有域都内部指向此目录)

/public_html/.htaccess

/public_html/www.example1.com/

/public_html/www.example2.com/

/public_html/www.example3.com/

有两种方法我我正在考虑,但希望得到其他人的意见:

第一个是将特定于域的重定向保留在上面定义的主 .htaccess 文件之外。因此,如果可能的话,我希望由 .htaccess 文件处理重定向,如下定义......

/public_html/www.example1.com/.htaccess

/public_html/www.example2.com/.htaccess

/public_html/www.example3.com/.htaccess

...如果这不可行,我将解决重写为PHP 文件将重定向移交给 PHP。我想这并不是以性能为导向的,但另一方面它会让我有机会记录重定向并查看它们需要多长时间才能达到平稳状态。

一些说明:

  1. 我正在使用共享网络托管,因此任何与 Apache 相关的事情都需要通过 .htaccess 文件来完成。

  2. 主 .htaccess 文件中没有重定向/匹配,也永远不会有,因为两个域最终可能会尝试使用相同的重定向。

On shared web-hosting my software supports multiple domains (all domains point to the same public_html root directory).

What I want to do is keep redirects (and any RedirectMatch) in their own host specific/dedicated .htaccess file.

Visually the directory structure looks like this...

/public_html/ (all domains are pointed internally to this directory)

/public_html/.htaccess

/public_html/www.example1.com/

/public_html/www.example2.com/

/public_html/www.example3.com/

There are two approaches I'm considering though would appreciate input from others:

The first would be to keep domain specific redirects out of the main .htaccess file as defined above. So I'd like to have redirects handled by the .htaccess files as defined by below if possible...

/public_html/www.example1.com/.htaccess

/public_html/www.example2.com/.htaccess

/public_html/www.example3.com/.htaccess

...if this is not feasible I'll settle for a rewrite to a PHP file to hand off redirects to PHP instead. I imagine this isn't as performance oriented though on the other hand it would give me the opportunity to log redirects and see how long it takes them to level off.

Some clarifications:

  1. I'm using shared web hosting so anything Apache related needs to be done through .htaccess files only.

  2. There are no redirects/matches in the master .htaccess file nor will there ever be since two domains may eventually attempt to use the same redirect.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长伴 2025-01-14 11:23:28

由于您位于共享主机上,因此您无法承担任何有关conf文件的解决方案(顺便说一句,这更好)。所以不会费心去列出它们。执行上述操作的最佳方法如下:

编写代码时要记住,没有任何域共享服务器上的任何类型的文件/数据。与域相关的每个文件/数据都保存在名称等于其域名的文件夹下。

下面的代码经过测试(静态和非静态):

RewritEngine on
RewriteBase /

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

并将以下任一内容添加到上面:

对于静态执行:

RewriteCond %{HTTP_HOST} ^www\.(example1|example2|example3)(\.com)$ [NC]
RewriteRule ^(.*)$ /www.%1%2/$1 [L]

对于静态执行:如果您想访问没有 www 的网站,

RewriteCond %{HTTP_HOST} ^(www\.)?(example1|example2|example3)(\.com)$ [NC]
RewriteRule ^(.*)$ /%1%2%3/$1 [L]

对于非静态执行:这是一个更好的 sol

RewriteRule ^(.*)$ /%{HTTP_HOST}/$1 [L]

以上所有内容都可以是将 URI 重定向到其特定域的文件夹。所有其他特定于域的重写都可以在相应的文件夹中处理。

如果您的 URI 不带 www,即 example1.com,请将 ^www\.(example1|example2|example3)(\.com)$ 更改为 ^( www\.)?(example1|example2|example3)(\.com)$

Since you are on shared host, You cannot afford to have any solutions concerning conf files (which BTW are better). So wont bother to list them. Best way to do the above is like this:

The code was written keeping in mind that none of the domains share any kind of file/data on the server. Every file/data pertaining to a domain is kept under a folder having the name equal to its domainname.

The code below is tested(both static and non static):

RewritEngine on
RewriteBase /

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

And add either of the following to the above:

for doing it statically:

RewriteCond %{HTTP_HOST} ^www\.(example1|example2|example3)(\.com)$ [NC]
RewriteRule ^(.*)$ /www.%1%2/$1 [L]

for doing it statically: and also if you want to access the site without www

RewriteCond %{HTTP_HOST} ^(www\.)?(example1|example2|example3)(\.com)$ [NC]
RewriteRule ^(.*)$ /%1%2%3/$1 [L]

for Non-statically do it: this is a better sol

RewriteRule ^(.*)$ /%{HTTP_HOST}/$1 [L]

All the above will do is redirect URI to their specific domain's folder. All other domain specific rewrites can be handled in the respective folders.

If you have URIs without the www, i.e. example1.com change ^www\.(example1|example2|example3)(\.com)$ to ^(www\.)?(example1|example2|example3)(\.com)$

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文