【问题标题】:RewriteRule for folder in folder文件夹中文件夹的 RewriteRule
【发布时间】:2014-08-26 22:09:58
【问题描述】:

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

<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 包含其他项目,例如:004、003、001 每个项目也包含 public_html 文件夹,带有 index.php 文件。

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

当我输入 URL : 001.example.com 文件的路径应该是:/home/git/www/public_html/001/public_html/index.php

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite subdomain


    【解决方案1】:

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

    DocumentRoot /home/git/
    

    然后限制对public_html的直接访问

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

    规范化 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]
    

    【讨论】:

    • 我不能使用 DocumentRoot /home/git/ 因为我有服务器的下一个结构: /home/git/www/public_html/ public_html 包含其他项目,例如:004、003、001 每个项目也包含 public_html 文件夹,带有 index.php 文件。当我输入 URL : 004.example.com 时,我需要使用 /home/git/www/public_html/004/public_html/index.php 文件。当我输入 URL : 001.example.com 文件路径应该是:/home/git/www/public_html/001/public_html/index.php
    猜你喜欢
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    • 2011-01-20
    相关资源
    最近更新 更多