【问题标题】:url rewriting with apache2 / php (symfony2)使用 apache2 / php (symfony2) 重写 url
【发布时间】:2013-08-13 19:58:54
【问题描述】:

考虑以下网址:

http://dns/ePhotograph/web/app.php/home

我想使用 URL 重写,以便可以从以下 URL 访问它:

http://dns/home

我怎样才能用 apache 做到这一点?

编辑

这是我的虚拟主机文件:

    <VirtualHost 88.191.157.10:80>
 ServerName myurl.fr
ServerAlias www.myurl.fr
ServerAdmin email@gmail.com
DocumentRoot /var/www/ePhotograph/web
DirectoryIndex app.php
<Directory "/var/www/ePhotograph/web">
  AllowOverride All
  Allow from All
</Directory>

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>

【问题讨论】:

    标签: php apache symfony


    【解决方案1】:

    在您的虚拟主机配置中,您需要将文档根目录指向web/,并将索引设置为app.php。然后使用重写规则将所有url推送到app.php

    <VirtualHost *:80>
      ServerName www.example.com
      ServerAlias www.example.com
      DocumentRoot "c:\wamp\www\symfony\web"
      DirectoryIndex app.php
      <Directory "c:\wamp\www\symfony\web">
        AllowOverride All
        Allow from All
      </Directory>
    
      <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ app.php [QSA,L]
      </IfModule>
    </VirtualHost>
    

    【讨论】:

    • 我猜这段代码应该添加到 httpd-vhosts.conf 文件中?在我的服务器上找不到它。我只是不明白。而且 httpd.conf 文件是空的。我错过了什么?
    • 好的,我快到了。我花了一段时间才发现在 Ubuntu 中我必须将一个 vhost 文件添加到启用站点的文件夹中。无论如何,当我浏览到dns/home 时,我得到一个 404。如果我浏览到dns/app.php/home 它工作正常,这意味着至少我的虚拟主机文件被考虑在内。但是重写规则不起作用。我认为这是因为 Symfony 路由器正在接管。我该如何解决?
    • @Sam 检查以确保您启用了 mod_rewrite 并重新启动 apache。
    • 是的,它已启用。我已经检查过了。事实上,当我浏览到dns 时,我被重定向到dns/app.php,所以它正在工作。但这不是我想要的。我想浏览到dns/home 并被重定向到dns/app.php/home,这不会发生
    • 我以为我已经成功了,但我错了。我已经编辑了我的帖子,所以你可以看到我的 vhost 文件的内容。如果您浏览到 URL samuel-berthelot.fr/app.php/accueil 那么它可以工作,但是一旦您删除 app.php 部分,它就会失败。但是我已经按照您的建议进行了重写。知道我错过了什么吗?
    猜你喜欢
    • 2010-11-11
    • 1970-01-01
    • 2014-07-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-08
    • 2013-04-29
    相关资源
    最近更新 更多