htaccess将所有子域重定向到同一目录

编程入门 行业动态 更新时间:2024-10-22 08:22:00
本文介绍了htaccess将所有子域重定向到同一目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

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

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

for example, if some visits sub1.example it will keep the URL but show example/subdomains/sub1 and if the sub1 directory does not exist, it will show example/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$ 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\$ RewriteCond %{REQUEST_URI} !^/subdomains [NC] RewriteCond %{REQUEST_URI} !^/404 [NC] RewriteRule ^(.+)$ /subdomains/%1/ [L]

  • 当我们遇到带有子域的请求时,
  • 我们还没有将其重写为/subdomains
  • 我们还没有将其重写为/404
  • 然后将其重写为/subdomains/{subdomain}/
  • 所以,如果请求是

    foo.example/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\$ 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]

  • 当我们遇到带有子域的请求时,
  • 我们已经将其重写为/subdomains
  • 我们还没有将其重写为/404
  • 它不作为文件存在
  • 它不存在于目录中
  • 并且它不以符号链接的形式存在
  • 然后将其重写为/404
  • 所以,如果请求是

    bar.example/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)测试环境

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

    更多推荐

    htaccess将所有子域重定向到同一目录

    本文发布于:2023-10-31 16:24:23,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1546624.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:重定向   目录   htaccess

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!