您可以使用重写规则使所有子文件夹子域吗?

问题描述 投票:2回答:1

因此,如果您具有这样的结构:

public_html/folder/example.com/index.php
public_html/folder/example.com/subdomain1/index.php
public_html/folder/example.com/subdomain2/index.php
public_html/folder/example.com/subdomain3/index.php

并且您希望每个“子域”文件夹都在http://subdomain1.example.com而不是http://example.com/subdomain1/ ...上可见

您能否将通配符子域DNS设置与仅.htaccess文件结合使用来实现?

例如,我通过cPanel添加了通配符子域,并且在我的.htaccess文件(example.com文件夹的根目录)中:

<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} ^([^.]+).example.com$ [NC]
RewriteCond %{REQUEST_URI} !%1
RewriteRule ^(.*)$ /%1/$1 [L]
</IfModule>

但是每次我尝试通过子域URL访问其中一个子文件夹时,都会出现500错误。

我缺少什么吗?我使用.htaccess测试工具来确保规则正确。我知道已启用mod_rewrite,并且Apache设置正确。例如,其中的index.php是否存在问题?

我也仍然可以访问http://example.com/subdomain1/并查看站点。它不会像我想的那样重定向到子域。

向正确方向提供的任何帮助将不胜感激。此时,我已经在搜索和测试不同的规则了几个小时。我确实联系了主机并确认我具有所有适当的权限和设置。

[如果我错过了-这里的想法是,每添加一个子文件夹(除非有特殊的例外),它都应该可以作为子域地址自动访问-无需在cPanel / DNS中添加任何手动子域。

/// 编辑 //// >>

下面是public_html /根目录中的.htaccess文件。 public_html / folder /目录中没有.htaccess。

# ----------------------------------------------------------------------
# CORS-enabled images (@crossorigin)
# ----------------------------------------------------------------------
# Send CORS headers if browsers request them; enabled by default for images.
# developer.mozilla.org/en/CORS_Enabled_Image
# blog.chromium.org/2011/07/using-cross-domain-images-in-webgl-and.html
# hacks.mozilla.org/2011/11/using-cors-to-load-webgl-textures-from-cross-domain-images/
# wiki.mozilla.org/Security/Reviews/crossoriginAttribute
<IfModule mod_setenvif.c>
  <IfModule mod_headers.c>
    # mod_headers, y u no match by Content-Type?!
    <FilesMatch "\.(gif|png|jpe?g|svg|svgz|ico|webp)$">
      SetEnvIf Origin ":" IS_CORS
      Header set Access-Control-Allow-Origin "*" env=IS_CORS
    </FilesMatch>
  </IfModule>
</IfModule>
# ----------------------------------------------------------------------
# Webfont access
# ----------------------------------------------------------------------
# Allow access from all domains for webfonts.
# Alternatively you could only whitelist your
# subdomains like "subdomain.example.com".
<IfModule mod_headers.c>
  <FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$">
    Header set Access-Control-Allow-Origin "*"
  </FilesMatch>
</IfModule>

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

AddType audio/ogg ogg ogv
AddType video/ogg ogg ogv

因此,如果您具有这样的结构:public_html / folder / example.com / index.php public_html / folder / example.com / subdomain1 / index.php public_html / folder / example.com / subdomain2 / index.php public_html / ...

apache .htaccess mod-rewrite dns wildcard-subdomain
1个回答
1
投票
RewriteCond %{HTTP_HOST} ^([^.]+).example.com$ [NC]
RewriteCond %{REQUEST_URI} !%1
RewriteRule ^(.*)$ /%1/$1 [L]
© www.soinside.com 2019 - 2024. All rights reserved.