Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alias and mod_rewrite

I'm trying to get Symfony1.4 and Symfony2 working on the same host in Apache (2.2.22) - I think the problem is that both are using mod_rewrite to direct the request to a php controller/script. Here is my config

httpd.conf

# Symfony 1.4
<VirtualHost *:80>
  DocumentRoot "d:/wamp/www/wlnew/web"
  DirectoryIndex index.php
  <Directory "d:/wamp/www/wlnew/web">
    AllowOverride All
    Allow from All
  </Directory>

  Alias /sf d:/wamp/www/wlnew/lib/vendor/symfony/data/web/sf
  <Directory "d:/wamp/www/wlnew/lib/vendor/symfony/data/web/sf">
    AllowOverride All
    Allow from All
  </Directory>
</VirtualHost>
# Symfony 2
Alias /another "d:/wamp/www/another/web/"
<Directory "d:/wamp/www/another/web">
    Options Indexes FollowSymlinks
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

And then each version of Symfony has a .htaccess which is used to re-write the request

Symfony 1 .htaccess

Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
  RewriteEngine On

  RewriteRule ^$ index.html [QSA]
  RewriteRule ^([^.]+)$ $1.html [QSA]
  RewriteCond %{REQUEST_FILENAME} !-f

  # no, so we redirect to our front web controller
  RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

Symfony 2 .htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On

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

I want to be able to use localhost/ to redirect to Symfony 1 front controller index.php but when I use localhost/another all requests should be forwarded to the symfony2 front controller app.php but they aren't - they are forwarded to the symfony1 front controller (index.php). If I use the file name of the controller for Symfony2 it works ie localhost/another/app.php

How can I get apache to forward requests to the Symfony2 controller when i use the /another alias ?

Update

I enabled logging of rewrite logging ... this is what I got :

[perdir D:/wamp/www/another/web/] strip per-dir prefix: D:/wamp/www/another/web/ -> 
[perdir D:/wamp/www/another/web/] applying pattern '^(.*)$' to uri ''
[perdir D:/wamp/www/another/web/] RewriteCond: input='D:/wamp/www/another/web/' pattern='!-f' => matched
[perdir D:/wamp/www/another/web/] rewrite '' -> 'app.php'
[perdir D:/wamp/www/another/web/] add per-dir prefix: app.php -> D:/wamp/www/another/web/app.php
[perdir D:/wamp/www/another/web/] internal redirect with D:/wamp/www/another/web/app.php [INTERNAL REDIRECT]
[perdir D:/wamp/www/wlnew/web/] add path info postfix: D:/wamp/www/wlnew/web/wamp -> D:/wamp/www/wlnew/web/wamp/www/another/web/app.php
[perdir D:/wamp/www/wlnew/web/] strip per-dir prefix: D:/wamp/www/wlnew/web/wamp/www/another/web/app.php -> wamp/www/another/web/app.php
[perdir D:/wamp/www/wlnew/web/] applying pattern '^$' to uri 'wamp/www/another/web/app.php'
[perdir D:/wamp/www/wlnew/web/] add path info postfix: D:/wamp/www/wlnew/web/wamp -> D:/wamp/www/wlnew/web/wamp/www/another/web/app.php
[perdir D:/wamp/www/wlnew/web/] strip per-dir prefix: D:/wamp/www/wlnew/web/wamp/www/another/web/app.php -> wamp/www/another/web/app.php
[perdir D:/wamp/www/wlnew/web/] applying pattern '^([^.]+)$' to uri 'wamp/www/another/web/app.php'
[perdir D:/wamp/www/wlnew/web/] add path info postfix: D:/wamp/www/wlnew/web/wamp -> D:/wamp/www/wlnew/web/wamp/www/another/web/app.php
[perdir D:/wamp/www/wlnew/web/] strip per-dir prefix: D:/wamp/www/wlnew/web/wamp/www/another/web/app.php -> wamp/www/another/web/app.php
[perdir D:/wamp/www/wlnew/web/] applying pattern '^(.*)$' to uri 'wamp/www/another/web/app.php'
[perdir D:/wamp/www/wlnew/web/] RewriteCond: input='D:/wamp/www/wlnew/web/wamp' pattern='!-f' => matched
[perdir D:/wamp/www/wlnew/web/] rewrite 'wamp/www/another/web/app.php' -> 'index.php'
[perdir D:/wamp/www/wlnew/web/] add per-dir prefix: index.php -> D:/wamp/www/wlnew/web/index.php
[perdir D:/wamp/www/wlnew/web/] strip document_root prefix: D:/wamp/www/wlnew/web/index.php -> /index.php
[perdir D:/wamp/www/wlnew/web/] internal redirect with /index.php [INTERNAL REDIRECT]
[perdir D:/wamp/www/wlnew/web/] strip per-dir prefix: D:/wamp/www/wlnew/web/index.php -> index.php
[perdir D:/wamp/www/wlnew/web/] applying pattern '^$' to uri 'index.php'
[perdir D:/wamp/www/wlnew/web/] strip per-dir prefix: D:/wamp/www/wlnew/web/index.php -> index.php
[perdir D:/wamp/www/wlnew/web/] applying pattern '^([^.]+)$' to uri 'index.php'
[perdir D:/wamp/www/wlnew/web/] strip per-dir prefix: D:/wamp/www/wlnew/web/index.php -> index.php
[perdir D:/wamp/www/wlnew/web/] applying pattern '^(.*)$' to uri 'index.php'
[perdir D:/wamp/www/wlnew/web/] RewriteCond: input='D:/wamp/www/wlnew/web/index.php' pattern='!-f' => not-matched
[perdir D:/wamp/www/wlnew/web/] pass through D:/wamp/www/wlnew/web/index.php

It seems that the .htaccess for the Symfony2 (/another) is being read and redirects to app.php but then reads the other .htaccess and then redirects to index.php ... How can i get the rewrite to stop at app.php

like image 380
Manse Avatar asked Jan 22 '13 16:01

Manse


People also ask

What is mod_rewrite?

The term "mod_rewrite" refers to a module for the Apache web server, which “rewrites” or redirects requests to specified content. Basically, the module transforms incoming requests to a path in the web server's file system. This makes it possible to "rewrite" a URL.

What is mod_rewrite used for?

mod_rewrite provides a flexible and powerful way to manipulate URLs using an unlimited number of rules. Each rule can have an unlimited number of attached rule conditions, to allow you to rewrite URL based on server variables, environment variables, HTTP headers, or time stamps.

What is RewriteCond and RewriteRule?

There are two main directive of this module: RewriteCond & RewriteRule . RewriteRule is used to rewrite the url as the name signifies if all the conditions defined in RewriteCond are matching. One or more RewriteCond can precede a RewriteRule directive.


2 Answers

It seems the configuration requires RewriteBase needs to be set for both the .htaccess.

RewriteBase /another

This should work. This link helped in this answer.

Also, here are some notes through our discussion on chat on how other settings can affect and how RewriteCond can rectify infinite internal redirect loops. Might help other people. Overall, mod_rewrite is one difficult module.

like image 195
Nandeep Mali Avatar answered Nov 15 '22 08:11

Nandeep Mali


Try to move the Symfony 2 alias inside the VirtualHost and add another DirectoryIndex for sf2:

<VirtualHost *:80>
  # Symfony 1.4
  DocumentRoot "d:/wamp/www/wlnew/web"
  <Directory "d:/wamp/www/wlnew/web">
    DirectoryIndex index.php
    AllowOverride All
    Allow from All
  </Directory>

  Alias /sf d:/wamp/www/wlnew/lib/vendor/symfony/data/web/sf
  <Directory "d:/wamp/www/wlnew/lib/vendor/symfony/data/web/sf">
    AllowOverride All
    Allow from All
  </Directory>

  # Symfony 2
  Alias /another "d:/wamp/www/another/web/"
  <Directory "d:/wamp/www/another/web">
    DirectoryIndex app.php
    Options Indexes FollowSymlinks
    AllowOverride All
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>
like image 21
j0k Avatar answered Nov 15 '22 07:11

j0k