重写到另一个文档根目录

时间:2017-01-27 16:05:50

标签: php apache .htaccess url-rewriting

我正在开发一个应该取代旧网站的新网站。新网站将使用Phalcon并使用自己的路由系统。与前一个网站一样,可以使用www.mydomain.com访问新网站。新网站DocumentRoot为/var/www/mydomain-www,旧网站为/var/www/mydomain-platform

在这个“平台”内部有一些“网络服务”,许多其他应用程序(包括iOS和Android应用程序)正在使用。不幸的是,新网站也必须使用这些网络服务。 :-(

基本上每个API调用都有一个PHP文件,所有这些文件都位于旧网站的根目录中,特别是/var/www/mydomain-platform。我知道这是一团糟,但我必须处理它,我继承了它。在旧的网站根目录中,还有一个.htaccess文件,其中包含每个API调用的重写路由,以及正确的PHP文件。他们看起来像:

RewriteRule ^ws/event/games(.*)$ /ws_events.php$1
RewriteRule ^ws/favourites/list(.*)$ /ws_teams_favorites.php$1

我需要找到一种方法将www.mydomain.com/ws/(.*)的所有url重写到目录/var/www/mydomain-platform/ws,因为Web服务的文件在那里,我真的不想在根目录中移动它们新网站的目录。

我想创建一个指向目录var/www/mydomain.www/ws的符号链接/var/www/mydomain-platform/ws,但我确定下面的vhost配置不喜欢它,因为它会将所有内容重定向到/var/www/mydomain/public

我想将这些重定向内容放在.htaccess中,然后像这样离开vhost。

有什么想法吗?

<VirtualHost *:80>
    ServerName www.mydomain.com
    ServerAdmin dev@mydomain.co.uk
    DocumentRoot "/var/www/mydomain-www"
    ErrorLog "${APACHE_LOG_DIR}/www.mydomain.com-error_log"
    CustomLog "${APACHE_LOG_DIR}/www.mydomain.com-access_log" common

    AllowEncodedSlashes On
    AddDefaultCharset UTF-8

    <Directory /var/www/mydomain-www>
        AuthType None
        AllowOverride all
        Require all granted

        <IfModule rewrite_module>
            RewriteEngine on
            RewriteRule  ^$ public/ [L]
            RewriteRule  ((?s).*) public/$1 [B,NE,L]
        </IfModule>
    </Directory>

    <Directory /var/www/mydomain-www/public>
        <IfModule rewrite_module>
            RewriteEngine on
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^((?s).*)$ index.php?_url=/$1 [B,NE,QSA,L]
        </IfModule>
    </Directory>

    <FilesMatch "(?i)\.php$">
        Require all denied
    </FilesMatch>

    <FilesMatch "index\.php$">
        Require all granted
    </FilesMatch>

</VirtualHost>

1 个答案:

答案 0 :(得分:1)

您可以将ws文件夹移动到/var/www/mydomain-www目录,或在新目录中创建符号链接。然后,您需要更改/var/www/mydomain-www中的规则,以跳过前面/ws/的任何内容:

<Directory /var/www/mydomain-www>
    AuthType None
    AllowOverride all
    Require all granted

    <IfModule rewrite_module>
        RewriteEngine on
        RewriteRule  ^$ public/ [L]
        RewriteRule ^/?((?!ws2/|ws3/).*)$ public/$1 [B,NE,L,NC]
    </IfModule>
</Directory>