如何修改现有的apache配置以更改新应用程序的文档根目录

时间:2016-04-19 13:16:18

标签: apache

我对apache不是很有经验但是我已经设法在我的测试盒上运行了几个月的以下设置:

me@mydev:/etc/apache2/sites-enabled$ vim 000-default.conf 
<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

所以基本上,无论何时我在/ var / www / html下创建一个新文件夹,它都可以在我的浏览器中使用。 但现在我想尝试做以下事情:

  1. 我创建了一个新的应用程序,结构如下:

    /var/www/html/testrestapp/src/public/index.php

  2. 它有效,但我必须导航到:

    http://localhost/testrestapp/src/public/index.php/hello/sometestname
    

    然后应用程序返回“sometestname”

    但是我想知道如何配置apache以便我可以像这样导航:

    http://localhost/testrestapp/hello/sometestname
    

    并获得相同的结果。

    有人能指出我正确的方向吗?不确定用什么术语来谷歌这个。

    感谢。

    编辑1

    我做了以下事情:

    me@mydev:/etc/apache2/sites-enabled$ sudo a2enmod rewrite
    Enabling module rewrite.
    To activate the new configuration, you need to run:
      service apache2 restart
    

    然后尝试重启:

    me@mydev:/etc/apache2/sites-enabled$ service apache2 restart
    Job for apache2.service failed because the control process exited with error code. See "systemctl status apache2.service" and "journalctl -xe" for details.
    
    Apr 19 10:14:39 mydev apache2[9010]: * The apache2 configtest failed.
    Apr 19 10:14:39 mydev apache2[9010]: Output of config test was:
    Apr 19 10:14:39 mydev apache2[9010]: AH00526: Syntax error on line 14 of /etc/apache2/sites-enabled/000-default.conf:
    Apr 19 10:14:39 mydev apache2[9010]: RewriteBase: only valid in per-directory config files
    Apr 19 10:14:39 mydev apache2[9010]: Action 'configtest' failed.
    Apr 19 10:14:39 mydev apache2[9010]: The Apache error log may have more information.
    Apr 19 10:14:39 mydev systemd[1]: apache2.service: Control process exited, code=exited status=1
    Apr 19 10:14:39 mydev systemd[1]: Failed to start LSB: Apache2 web server.
    Apr 19 10:14:39 mydev systemd[1]: apache2.service: Unit entered failed state.
    Apr 19 10:14:39 mydev systemd[1]: apache2.service: Failed with result 'exit-code'.
    

    以下是我的/etc/apache2/sites-enabled/000-default.conf文件的样子:

     11         ServerAdmin webmaster@localhost
     12         DocumentRoot /var/www/html
     13         RewriteEngine  on
     14         RewriteBase    /testrestapp/
     15         RewriteRule    ^src/public/index.php/hello$  hello/  [R]
    

    编辑2:

    我的000-default.conf文件现在看起来像这样:

    <VirtualHost *:80>
            ServerAdmin webmaster@localhost
            DocumentRoot /var/www/html
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
    
    </VirtualHost>
    <Directory "/var/www/html/testrestapp/">
        RewriteEngine  on
        RewriteBase    /testrestapp/
        RewriteRule    ^src/public/index.php/hello$  hello/  [R]
    </Directory>
    

    Apache成功重启:

    me@mydev:/etc/apache2/sites-enabled$ sudo /etc/init.d/apache2 restart
    [ ok ] Restarting apache2 (via systemctl): apache2.service.
    

    但是当我导航到http://localhost/testrestapp/hello/jojo时,我收到404错误。

    这是我在access.log中看到的:

    127.0.0.1 - - [19 / Apr / 2016:13:50:04 -0400]“GET / testrestapp / hello / jojo HTTP / 1.1”404 507“ - ”“Mozilla / 5.0(X11; Linux x86_64) AppleWebKit / 537.36(KHTML,与Gecko一样)Chrome / 49.0.2623.110 Safari / 537.36“

1 个答案:

答案 0 :(得分:0)

在“/etc/apache2/sites-enabled/000-default.conf”中,使用mod_alias并添加以下内容:

alias "/testrestapp/hello" "/var/www/html/testrestapp/src/public/index.php/hello"