htaccess将所有子域重定向到同一目录 [英] htaccess redirect all subdomains to the same directory

查看:49
本文介绍了htaccess将所有子域重定向到同一目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够将所有子域重定向到一个文件夹:

I want to be able to redirect all subdomains to a folder:

RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$ 
RewriteRule (.+)$ "http://example.com/subdomains/%1" [L,P]

例如,如果某些访问 sub1.example.com ,它将保留URL,但显示 example.com/subdomains/sub1 ,并且sub1目录不存在,它将显示 example.com/404

for example, if some visits sub1.example.com it will keep the URL but show example.com/subdomains/sub1 and if the sub1 directory does not exist, it will show example.com/404

这可能吗?

我尝试了上面的代码,但向我显示了

I tried the above code but its showing me:

Forbidden
You don't have permission to access /index.php on this server.

Wordpress说:

Wordpress says:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

在我的htaccess文件顶部,是:

and at the top of my htaccess file, is:

RewriteEngine On
DirectoryIndex index.php

RewriteCond %{HTTP_HOST} admin.domain.com$
RewriteRule ^(.*)$ /admin/system/$1 [L]

推荐答案

当您使用完整的URL作为目标时,.htaccess以上的内容将在外部重定向呼叫.

Your above .htaccess would externally redirect the calls, as you use a full URL as the target.

在您的问题中,您说您想保留主机名,所以我认为这是必需的.

In your question you say you want to keep the hostname, so I will assume that is the requirement.

RewriteEngine On

1)将已知子域重写到/subdomains中的目录中

# Rewrite known subdomains to /subdomains/{subdomain}/
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteCond %{REQUEST_URI} !^/subdomains [NC]
RewriteCond %{REQUEST_URI} !^/404 [NC]
RewriteRule ^(.+)$ /subdomains/%1/ [L]

  1. 当我们遇到带有子域的请求时,
  2. 我们还没有将其重写为/subdomains
  3. 我们还没有将其重写为/404
  4. 然后将其重写为/subdomains/{subdomain}/

所以,如果请求是

http://foo.example.com/hello

浏览器中的URL保持不变,但在内部被映射到

the URL in the browser would stay the same, but internally be mapped to

/subdomains/foo/

2)将未知子域重写为/404

# Rewrite missing unknown subdomains to /404/
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteCond %{REQUEST_URI} ^/subdomains [NC]
RewriteCond %{REQUEST_URI} !^/404 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ /404/ [L]

  1. 当我们遇到带有子域的请求时,
  2. 我们已经将其重写为/subdomains
  3. 我们还没有将其重写为/404
  4. 它不作为文件存在
  5. 它不存在于目录中
  6. 并且它不以符号链接的形式存在
  7. 然后将其重写为/404

所以,如果请求是

http://bar.example.com/hello

浏览器中的URL保持不变,但在内部被映射到

the URL in the browser would stay the same, but internally be mapped to

/subdomains/bar/

由1)的第一个 RewriteRule

如果/subdomains/bar/不存在,则2)中的第二个 RewriteRule 将插入并在内部映射到

If /subdomains/bar/ does not exist, the second RewriteRule from 2) will kick in and internally map it to

/404/

3)测试环境

我实际上使用示例代码对所有这些进行了测试,可在此处找到: https://github.com/janpapenbrock/stackoverflow-36497197

这篇关于htaccess将所有子域重定向到同一目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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