2

I have the following directory structure for my local site.

mysite
|_ css
|_ images
    |_ logo.png
|_ js
|_ app
    |_ index.php
|_ .htaccess

I'm trying Apache URL rewriting to redirect to http://mysite.dev/app/index.php for non-existing directories. For example, http://mysite.dev/en/home will be rewritten into http://mysite.dev/app/index.php

I have tried this rewrite rule in my .htaccess file:

Options -Indexes
DirectoryIndex index.php index.html index.htm

<IfModule mod_rewrite.c>
   RewriteEngine on

   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteRule ^ app/index.php [L]   
</IfModule>

But it is rewritten for all existing directories and no-existing directories. That is, when I browse http://mysite.dev/mysite/images/logo.png, it is rewritten to http://mysite.dev/mysite/app/index.php

I added the line above the two RewriteCond lines, but it did not work.

RewriteRule ^(images|css|js) - [NC,L]

My goal is to skip the rule if any file or directory exists. How can I write RewriteCond and RewriteRule for this purpose?

[Edit]
I found the problem. It is because of the virtual host I added for my site. I have the following lines of code in httpd-vhost.conf:

<VirtualHost mysite.dev>
    DocumentRoot "C:/xampp/htdocs/mysite"
    ServerName mysite.dev
    ServerAlias mysite.dev
    <Directory "C:/xampp/htdocs/mysite">
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

When I browsed http://mysite.dev/images/logo.png, the RewriteCond is not working properly
But when I removed the virtual host and I browsed http://localhost/mysite/images/logo.png, it is working fine and I see the image correctly.

I'm not sure what is the problem and conflict of virtual host and RewriteCond.

Sithu
  • 4,752
  • 9
  • 64
  • 110
  • Where is your htaccess file? – Jon Lin Dec 15 '13 at 17:31
  • 1
    Are you sure that `images/logo.png` is visible to Apache? If not, Apache will treat it as non-existent. If you remove your RewriteRule, can your host serve `mydomain.com/images/logo.png`? – Andrew Schulman Dec 15 '13 at 22:25
  • turn on rewrite logging http://stackoverflow.com/questions/9632852/how-to-debug-apache-mod-rewrite – Andy Jones Dec 16 '13 at 02:49
  • @Sithu: Are `css` and `images` real directories OR some symbolic links. – anubhava Dec 16 '13 at 06:32
  • `css`, `images` and `js` are real directories and `images/logo.png` is also visible to Apache. I have to check the apache config or Windows host config because it is perfectly working in the other PC without even `RewriteRule ^(images|css|js)`. – Sithu Dec 16 '13 at 08:38

1 Answers1

0

In your virtual host directive you need to have an AllowOverride directive:

<VirtualHost mysite.dev>
    DocumentRoot "C:/xampp/htdocs/mysite"
    ServerName mysite.dev
    ServerAlias mysite.dev
    <Directory "C:/xampp/htdocs/mysite">
        AllowOverride FileInfo

        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>
Jon
  • 12,684
  • 4
  • 31
  • 44