RewriteRule用于文件夹中的文件夹

时间:2014-08-26 22:09:59

标签: regex apache .htaccess mod-rewrite subdomain

我的网站中有以下记录 - 可用/默认:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName example.com
        DocumentRoot /home/git/www/public_html
        <Directory />
                Options FollowSymLinks
                AllowOverride All
        </Directory>
        <Directory /home/git/www/public_html>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride  All
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel debug

        CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride All
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

ServerAlias *.example.com
  RewriteEngine On
  RewriteCond %{HTTP_HOST} !^www.example.com$
  RewriteCond %{HTTP_HOST} ^((.*)\.)example.com$
  RewriteRule /(.*) /%2/$1

</VirtualHost>

但是,我需要将我的网站重定向到/(.*)/public_html,其中找到index.php文件。

如何将请求重定向到子文件夹,以显示子域的内容?

我无法使用DocumentRoot / home / git /,因为我有下一个服务器结构:

/home/git/www/public_html/ public_html包含其他项目,e.x:004,003,001 每个项目也包含public_html文件夹,带有index.php文件。

当我输入网址:004.example.com时,我需要使用/home/git/www/public_html/004/public_html/index.php文件。

当我输入网址时:001.example.com文件的路径应为:/home/git/www/public_html/001/public_html/index.php

1 个答案:

答案 0 :(得分:0)

首先尝试将您的文档根目录设置为git

DocumentRoot /home/git/

然后限制对public_html的直接访问

RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+(.*)public_html/([^\?\ ]*)
RewriteRule ^ http://%1.example.com/%2 [L,R=301]

Canonicallize www:

RewriteCond %{HTTP_HOST} ^$ [OR]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

然后在内部重写到public_html

RewriteCond %{REQUEST_URI} !/public_html/
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule ^(.*)$ /%1/public_html/$1 [L]