子域的重写规则 [英] Rewrite rule for subdomains

查看:114
本文介绍了子域的重写规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以对子域使用.htaccess指向主域上的文件?
我将example.com作为主域,并创建了子域:one.example.com,two.example.com等(通过DirectAdmin)。子域应指向主域上的app.php,但子域应保留在URL中。

Is it possible to use .htaccess for a subdomain to point to a file on the main domain? I have example.com as a main domain and created subdomains: one.example.com, two.example.com, etc through DirectAdmin. The subdomains should point to app.php on the main domain but in the url the subdomain should persist.

因此,导航至one.example.com应该指向app。 php,它位于example.com的主域中,但url仍应显示one.example.com

So navigating to one.example.com should be pointing to app.php which lives in the main domain example.com, but the url should still show one.example.com

我已经查看并尝试了许多关于的重写规则 .htaccess将子域重写到目录和其他地方。
当前,我在子域的子文件夹中的.htaccess文件中具有以下重写规则:

I have looked and tried a lot of the rewrite rules found on .htaccess rewrite subdomain to directory and elsewhere. Currently I have this rewrite rule in a .htaccess file in the subfolder of the subdomain:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)$ http://example.com/app.php$1 [L,NC,QSA]

这会将URL重定向并更改为example.com/app.php,而我希望显示子域:one.example.com/whatever

This redirects and changes the url to example.com/app.php while I would like the url to show the subdomain: one.example.com/whatever

是否可以使用.htaccess做到这一点,或者还有更好的方法吗?

Is it possible to do this with .htaccess ,or is there maybe even a better way to do this?

推荐答案

A RewriteRule 在另一台服务器上具有目标将始终执行重定向

A RewriteRule with a target on another server will always do a redirect


绝对URL

如果指定了绝对URL,则mod_rewrite 检查主机名是否与当前主机匹配。如果是这样,则将方案和主机名删除,并将得到的路径视为URL路径。 否则,将对给定的URL进行外部重定向。要强制外部重定向回到当前主机,请参见下面的[R]标志。

Absolute URL
If an absolute URL is specified, mod_rewrite checks to see whether the hostname matches the current host. If it does, the scheme and hostname are stripped out and the resulting path is treated as a URL-path. Otherwise, an external redirect is performed for the given URL. To force an external redirect back to the current host, see the [R] flag below.






要将URL http://example.com/app.php 映射到您的子域而不进行重定向,您可以尝试 P | proxy 标志


To map the URL http://example.com/app.php into your subdomains without redirecting, you might try the P|proxy flag

RewriteRule ^(.*)$ http://example.com/app.php/$1 [P,QSA]

还要牢记最后一点


注意:必须启用mod_proxy才能使用此标志。

Note: mod_proxy must be enabled in order to use this flag.

通常,这可以通过

a2enmod proxy proxy_http

如果目标URL为 https:// ... ,则还必须启用mod_proxy_connect,并使用 SSLProxyEngine

If the target URL is https://..., you must also enable mod_proxy_connect, and activate SSL in your main or virtual host config with SSLProxyEngine

SSLProxyEngine on






根据您的需求, ProxyPass 也是可行的解决方案。


Depending on your needs, ProxyPass might be a viable solution too.

这篇关于子域的重写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆