HTACCESS:如何 URL 重写子域? [英] HTACCESS: How to URL rewrite a sub-subdomain?

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

问题描述

我有子域 apps.domain.com 指向 domain.com/apps

我想 URL 重写 appName.apps.domain.com,其中 appName 是指向 domain.com/apps/appName 的变量代码>

I would like to URL rewrite appName.apps.domain.com, where appName is the variable pointing to domain.com/apps/appName

  • 我正在寻找一个内部转发(浏览器的 URL 没有变化)

我的 .htaccess 文件中应该有什么?我应该把它保存在哪里?在 /apps/ 文件夹中?还是在 root 文件夹中?

What should I have in my .htaccess file and where should I save it? Within /apps/ folder? Or within the root folder?

最后,如果使用 .htaccess 文件无法实现,我该怎么做?我使用 Linux Godaddy 服务器作为主机.

Lastly, if it is not possible with an .htaccess file, how can I do this? I'm using a Linux godaddy server as host.

**** 问题更新于 07/08/2018;添加了更多细节 ****

  • 我使用的是 GoDaddy 共享托管
  • 它目前托管多个域
  • 因此,目录结构为 public_html/domain.com/,其中 /domain.com/ 是托管域名的名称
  • 子子域专用于 1 个特定域
  • I am using GoDaddy SHARED hosting
  • It is currently hosting multiple domains
  • Thus, the directory structure is public_html/domain.com/, where /domain.com/ is the name of the hosted domain name(s)
  • The sub-subdomain is exclusive to 1 specific domain

示例:

  • 如果域名是domain.com
  • 有多个名称,但举个例子.如果应用程序的名称是awesome
  • 如果 uri 是:awesome.apps.domain.com 将指向...public_html/domain.com/apps/awesome/
  • 如果 uri 是:ubertz.apps.domain.com 将指向...public_html/domain.com/apps/ubertz/
  • 等等...
  • If the domain name is domain.com
  • There's multiple names, but to give an example. if the name of the app is awesome
  • If the uri is: awesome.apps.domain.com will point to...public_html/domain.com/apps/awesome/
  • If the uri is: ubertz.apps.domain.com will point to...public_html/domain.com/apps/ubertz/
  • And so on...

推荐答案

创建通配符子域

您需要做的第一件事是创建一个通配符 *.apps.domain.com 子域.

  1. (如果您使用域名服务器,请跳过此步骤!) 登录您的域名注册商,并为 创建一个A 记录*.apps.domain.com (是的,这是一个星号) 并将其指向服务器 IP 地址.请注意,DNS 最多可能需要 48 小时才能传播.

  1. (If you're using Nameservers, skip this step!) Log in to your domain name registrar, and create an A record for *.apps.domain.com (yeah, that's an asterisk) and point it to the server IP address. Note that DNS can take up to 48 hours to propagate.

登录您的网络托管帐户并转到部分下的菜单子域".创建一个子域 *.apps.domain.com,它指向/public_html/domain.com/apps" 文件夹作为它的<强>文档根目录.并等到传播结束.

Log in your web hosting account and go to the menu 'Subdomains' under Domains section. Create a Subdomain *.apps.domain.com that's pointed to the "/public_html/domain.com/apps" folder as its Document Root. And wait until the propagation is over.

访问 如何在 cPanel 中创建通配符子域如何创建子域- 子域 了解更多信息.如果您的服务器配置不允许您创建通配符子域,那么只需创建 *.domain.com 并将其指向 /public_html/domain.com/apps".

Visit How to create wildcard subdomain in cPanel and How to Create a Sub-Subdomain for more info. If your server's configuration didn't allow you to create a wildcard sub-subdomain then just create *.domain.com and point it to "/public_html/domain.com/apps".

将这些指令放在通配符 *.apps.domain.com 的文档根目录的 .htaccess 文件中,在本例中为 "/public_html/domain.com/apps".

Place these directives in the .htaccess file of the document root of the wildcard *.apps.domain.com which in this case is "/public_html/domain.com/apps".

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.+).apps.domain.com
RewriteRule (.*) http://domain.com/apps/%1/$1 [QSA,P,L]

%1(.+) 的编号反向引用,$1(.*).如果您从特定域重写 URL,则需要包含 [P] 标志http://domain.com/apps/%1/$1 在同一服务器中,否则它将被重定向.如果您要使用重写的通配符子域的查询字符串,请使用 [QSA] 标志.访问 P|proxyQSA|qsappend 了解更多信息.如果我忘记了什么,只需进行一些调整,除此之外,是服务器的错.

The %1 is the numbered backreference of (.+) and the $1 is of (.*). You need to include the [P] flag if you URL rewrite from a specific domain http://domain.com/apps/%1/$1 in the same server, without that it will be redirected. Use the [QSA] flag if you'll going to use the query string of a rewritten wildcard subdomain. Visit P|proxy and QSA|qsappend for more info about them. And just tweak some adjustment if I forgot something, other than that, is the server's fault.

您必须为将要存在的重复子域配置 .htaccess 文件,例如 otherwildcard.apps.domain.com另一个.wilcard.apps.domain.comother.deep.level.apps.domain.com 将按原样复制 apps.domain.com都具有相同的文档根目录.配置为重定向或告诉客户端这些子域不存在.

You must configure your .htaccess file for the duplicate subdomains that will be going to exist, such the otherwildcard.apps.domain.com, another.wilcard.apps.domain.com and the other.deep.level.apps.domain.com that will duplicate apps.domain.com as they're all have the same document root. Configure to redirect or to tell to the client that those subdomains aren't exist.

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

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