简体   繁体   中英

Apache RewriteCond and RewriteRule to skip for the existing files and directories

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 .

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM