logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Apache View previous topic :: View next topic
Reply to topic   Topic: RewriteCond to check file existence in alias location
Author
nutflakes



Joined: 13 Mar 2011
Posts: 2

PostPosted: Sun 13 Mar '11 19:18    Post subject: RewriteCond to check file existence in alias location Reply with quote

Hi there,

I do really have problems to get the mod_rewrite configuration working properly... so hopefully you will help me out!

The whole thing is based on the following file system.

Code:

project_1
    private
    public
        index.php
project_2
    private
        test.php
    public


I configured the vhost

Code:

<VirtualHost 127.0.0.1>
    ServerAdmin ...
    ServerName domain.test
    Options Indexes FollowSymLinks
    DocumentRoot "D:\path\project_1\public"
    Alias "/project_2" "D:\path\project_2"
    <Directory "D:\path\project_1\public">
        DirectoryIndex index.php
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>


Now I am trying to set up the following rule.
If a file is called that is not available in my document root several other possible locations should be searched for the ressource and if found the redirect should occur and stop searching.

Let's see this example call

Code:

Call: domain.test/bla.php
-> search for /project_1/public/bla.php
-> search for /project_1/private/bla.php
-> search for /project_2/public/bla.php
-> search for /project_2/private/bla.php
-> 404 if not found


To test the rules I used a hard coded path.

The following test-call works with the redirect to the given alias.

Code:

RewriteRule  ^(.*)  /project_2/private/test.php  [L]


But adding the condition to check the file existence first the rule is not executed any more. It seems to me that an alias in the condition is not taken...

Code:

RewriteCond /project_2/private/test.php -f
RewriteRule  ^(.*)  /project_2/private/test.php  [L]


How to get this search routine working without the possibility to check for existing files in an alias outside the document root?
I'm driving crazy with this Confused

Any help is very welcome!
Thanks a lot Cool

Regards from Munich,
Uwe
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7371
Location: Germany, Next to Hamburg

PostPosted: Wed 16 Mar '11 1:47    Post subject: Reply with quote

Hi Uwe,
I'm not an expert in mod rewrite, but I've never seen a method / function that looks if a file exists or not.
So I would do that my application (php file) and a rewrite rule that redirects every request of a file or directory that doesn't exist in the filesystem to that dispatch file

e.g. for your vhost
Code:

RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1


I guess it is easy for you in PHP do do that.

if you have any question or something else please ask again.

Best regards from Hamburg
Mario
Back to top
nutflakes



Joined: 13 Mar 2011
Posts: 2

PostPosted: Wed 16 Mar '11 12:00    Post subject: Reply with quote

Hi Mario,

thanks for your reply.

First you should know that it is a Zend application and my solution is only required for local development. A build script will later create the final project structure that will get deployed to the different servers.

As our versioned project structure is split into several directories and components this mod_rewrite modification would be a big help for me.

This link will show how it is possible to get it working for two directories, but not for three.
Serving one Apache site from two parallel directories

The main problem I have is to get this alias file check working in my windows environment. Primarily this will help to locate and include missing images, js files and xml's. All the php includes can be extended to fit our needs.

Best regards,
Uwe
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7371
Location: Germany, Next to Hamburg

PostPosted: Thu 17 Mar '11 1:53    Post subject: Reply with quote

Well in my example without that Alias inside the vhost a non existing directory or file would be rewritten to the index.php and added as path

e.g.
http://domain.test/project2/test.php would become
http://domain.test/index.php/project/test.php

inside index.php
Code:

print_r(preg_split("{/}",$_SERVER["PHP_SELF"]));


output:
Code:

Array
(
    [0] =>
    [1] => index.php
    [2] => project2
    [3] => test.php
)



Ok that is not a final script. But a good way to handle that. Even when it was multiple subfolders.
Back to top
Elan



Joined: 17 Jun 2011
Posts: 1

PostPosted: Fri 17 Jun '11 11:33    Post subject: RewriteCond to check file existence in alias location Reply with quote

Hi nutflakes,

I am trying the similar scenario. When condition is given, the redirection is not happening. If condition is not given, then it is working fine. Below is the configuration.Is there anything else have to be configured??


D:/test/theme/.htaccess :
Code:

Options +FollowSymLinks
RewriteEngine On
RewriteCond /test/theme/Bala/ -d
RewriteRule ^default/(.*)$ /test/theme/Bala/$1


httpd.conf :

Code:
Alias /test "D:/test"
<Directory "D:/test">
Options Indexes +FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Back to top
crumpysale56



Joined: 15 Jul 2011
Posts: 2

PostPosted: Fri 15 Jul '11 12:05    Post subject: Reply with quote

Well in my example without that Alias inside the vhost a non existing directory or file would be rewritten to the index.php and added as path.
Back to top


Reply to topic   Topic: RewriteCond to check file existence in alias location View previous topic :: View next topic
Post new topic   Forum Index -> Apache