安装在子文件夹中的App上的Apache2循环重定向

时间:2018-04-21 12:57:46

标签: apache mod-rewrite

我尝试在子文件夹(在“开发服务器”上)安装我的Symfony 4应用程序但是我在apache2配置方面遇到了一些问题(我用apache完成了noob)

所以这是我的结构 我的应用已安装在/var/www/html/rapp/ 我在/etc/apache2/sites-enabled/rapp.conf中有一个apache配置文件,其中包含以下内容

<VirtualHost *:80>
    DocumentRoot /var/www/html/rapp/public

    <Directory /var/www/html/rapp/public>
        AllowOverride All
        Order Allow,Deny
        Allow from All

        <IfModule mod_rewrite.c>
            Options -MultiViews
            RewriteEngine On
            RewriteBase /rapp/

            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule ^.*$ index.php [L,NC]

        </IfModule>
    </Directory>

    # uncomment the following lines if you install assets as symlinks
    # or run into problems when compiling LESS/Sass/CoffeeScript assets
    # <Directory /var/www/project>
    #     Options FollowSymlinks
    # </Directory>

    # optionally disable the RewriteEngine for the asset directories
    # which will allow apache to simply reply with a 404 when files are
    # not found instead of passing the request into the full symfony stack
    <Directory /var/www/html/rapp/public/bundles>
        <IfModule mod_rewrite.c>
            RewriteEngine Off
        </IfModule>
    </Directory>

    <Directory /var/www/html/rapp/public/build>
        <IfModule mod_rewrite.c>
            RewriteEngine Off
        </IfModule>
    </Directory>

    ErrorLog /var/log/apache2/project_error.log
    CustomLog /var/log/apache2/project_access.log combined

</VirtualHost>

在我的应用根文件夹(/var/www/html/rapp/)上,我有一个.htaccess文件,其中包含以下内容,可将http://xxx.xxx.xxx.xxx/rapp/网址重定向到我的应用

<IfModule mod_rewrite.c>
    Options -MultiViews
    RewriteEngine On
    RewriteBase /rapp/
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

我在/var/www/html/rapp/public中没有.htaccess,因为它必须由我的apache .conf文件中的Directory标记处理(我认为)

使用所有配置我在apache error.log中有这些错误

Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
r->uri = /rapp/public/public/public/public/public/public/public/public/public/public/
redirected from r->uri = /rapp/public/public/public/public/public/public/public/public/public/
redirected from r->uri = /rapp/public/public/public/public/public/public/public/public/
redirected from r->uri = /rapp/public/public/public/public/public/public/public/
redirected from r->uri = /rapp/public/public/public/public/public/public/
redirected from r->uri = /rapp/public/public/public/public/public/
redirected from r->uri = /rapp/public/public/public/public/
redirected from r->uri = /rapp/public/public/public/
redirected from r->uri = /rapp/public/public/
redirected from r->uri = /rapp/public/
redirected from r->uri = /rapp/

我真的不明白为什么它会搜索那么多公共子文件夹。

最后,我想做的是: 当我转到http://xxx.xxx.xxx.xxx/rapp/时,它会在我/var/www/html/rapp/的应用中重定向我。 所以我想要安装应用程序的子文件夹,而不是URL中的public/index.phppublic/部分。

1 个答案:

答案 0 :(得分:0)

问题是您的规则模式(.*)也匹配目标路径/public/$1,并以循环模式将目标重写为自身。您需要在RewriteRule中排除目标路径以避免重写循环错误。

<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /rapp/
RewriteRule ^((?!public/).*)$ public/$1 [L]
</IfModule>

或者您也可以使用条件来排除目标路径

<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /rapp/
RewriteRule %{REQUEST_URI} !/public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>