【问题标题】:Apache, root, subdomain and subdirectoryApache、根、子域和子目录
【发布时间】:2017-08-04 13:10:54
【问题描述】:

我创建了一个在 root 上有多个目录的服务器:

  • /网站
  • /论坛
  • /维基
  • 等等……

当我访问该站点时,URL 是 mysite.org/site 或 mysite.org/forum。

我想要的是将我的 .htaccess 配置为像这样:

  • 站点目录用作根目录:URL是mysite.org/blabla,但显示的是mysite.org/site/blabla的内容
  • 其他目录用作子域:URL 是 forum.mysite.org 但它显示 mysite.org/forum

我无法让这个工作......

目前我的 .htaccess 是这样的:

RewriteEngine On

# Map http://www.example.com to /site.
RewriteRule ^$ /site/ [L]

# Map http://www.example.com/x to /site/x unless there is a x in the web root.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/site/
RewriteRule ^(.*)$ /site/$1

# Add trailing slash to directories without them so DirectoryIndex works.
# This does not expose the internal URL.
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} !/$
RewriteRule ^(.*)$ $1/

# Disable auto-adding slashes to directories without them, since this happens
# after mod_rewrite and exposes the rewritten internal URL, e.g. turning
# http://www.example.com/about into http://www.example.com/site/about
DirectorySlash off

有人可以帮帮我吗?

【问题讨论】:

    标签: apache .htaccess


    【解决方案1】:

    我不确定 .htaccess 是否可以做到这一点。无论如何,我认为最好/正确的方法是将子目录拆分到由同一个 apache 服务的不同网站。

    例如:

    /var/www/mysite.org/wiki/* 移至 -> /var/www/wiki.mysite.org/*
    /var/www/mysite.org/forum/* 移动到 -> /var/www/forum.mysite.org/*
    保持/var/www/mysite.org there

    然后构建 apache 配置,例如:

    mysite.conf:

    # domain: mysite.org
    # public: /var/www/mysite.org
    
    <VirtualHost *:80>
      # Admin email, Server Name (domain name), and any aliases
      ServerAdmin admin@mysite.org
      ServerName  mysite.org
      ServerAlias www.mysite.org *.mysite.org mysite.org
    
      # Index file and Document Root (where the public files are located)
      DirectoryIndex index.html index.php
      DocumentRoot /var/www/mysite.org/
    
      <Directory /var/www/mysite.org>
        Options -Indexes
        Options +FollowSymLinks
        Options -Includes
        Options -ExecCGI
        AllowOverride All
        Require all granted
      </Directory>
    
      # Log file locations
      LogLevel warn
      ErrorLog  /var/log/apache2/mysite.org_error.log
      CustomLog /var/log/apache2/mysite.org_access.log combined
    </VirtualHost>
    

    wiki.conf:

    # domain: wiki.mysite.org
    # public: /var/www/wiki.mysite.org
    
    <VirtualHost *:80>
      # Admin email, Server Name (domain name), and any aliases
      ServerAdmin admin@mysite.org
      ServerName  wiki.mysite.org
      ServerAlias wiki.mysite.org
    
      # Index file and Document Root (where the public files are located)
      DirectoryIndex index.html index.php
      DocumentRoot /var/www/wiki.mysite.org/
    
      <Directory /var/www/wiki.mysite.org>
        Options -Indexes
        Options -FollowSymLinks
        Options -Includes
        Options -ExecCGI
        AllowOverride All
        Require all granted
      </Directory>
    
      # Log file locations
      LogLevel warn
      ErrorLog  /var/log/apache2/wiki.mysite.org_error.log
      CustomLog /var/log/apache2/wiki.mysite.org_access.log combined
    </VirtualHost>
    

    通过这种方式,您可以为每个站点指定不同的设置(日志文件、配置包含等),并做一些很酷的事情,例如使用 mpm_itk 以不同的 UID/GID 运行每个站点。

    【讨论】:

    • 很遗憾,我不在专用服务器上,因此无法更改 conf 文件。这就是为什么我想通过编辑 htaccess 来做到这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-30
    • 1970-01-01
    • 1970-01-01
    • 2017-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多