0

Consider this workflow:

User makes a request to website.com/lolmyblogpost

My .htacces is all like...

RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule .* /index.php

Where in index.php im going to search a file tree of templates for lolmyblogpost.html returning:

/path/to/lolmyblogpost.html

So in my master template I can:

{include file="{$pathToTemplate}"}

How do I search a directory tree for a file and return the file path?

8
  • 1
    Ok. so what is your question!!!!!!!!!!!!!!! Commented Jan 6, 2013 at 4:57
  • @mamdouhalramadan The question is how do I "PHP: Search Directory Tree for File and Return File Path" Commented Jan 6, 2013 at 4:58
  • In Apache, PHP, Ruby, ... what language? Commented Jan 6, 2013 at 4:59
  • 1
    Why don't you know where your templates are? Commented Jan 6, 2013 at 5:02
  • 2
    Ah.. then, you are going about it the wrong way. I'll write an answer. Commented Jan 6, 2013 at 5:05

1 Answer 1

1

What you really want is "look back" support with a default type. Set up your Apache virtual host (something) like this:

<VirtualHost *:80>
    ServerName  example.com:80
    ServerAlias www.example.com
    DocumentRoot    "/path/to/root/dir"
    AddDefaultCharset UTF-8
    ErrorDocument 403 "/403.php"
    ErrorDocument 404 "/404.php"
    <Directory /path/to/root/dir>
            Options Indexes FollowSymLinks
            DefaultType application/x-httpd-php
            AllowOverride All
            Order deny,allow
            Allow from all
            AddOutputFilterByType DEFLATE application/javascript text/css text/html text/plain text/xml
            RewriteEngine On
            RewriteBase /
            RewriteCond %{REQUEST_FILENAME} !.index.ph.*
            RewriteRule ^(.*)$  /index.php
    </Directory>
</VirtualHost>

The important line is DefaultType application/x-httpd-php, which means you can now get rid of the .php file extension.

You can now use a URL like http://example.com/this_is_a_php_page and you can also use http://example.com/this_is_a_php_page/with_a_path_info_var.

So, on this_is_a_php_page (which is really a .php file without an extension) you can use $_SERVER['PATH_INFO'] to check for and vars that are being passed in the URI.

Edit: Added the RewriteEngine and rule to push everything to index.php. This means you now have a (real) single page on the server called index.php which will need to check the $_SERVER['REDIRECT_URL'] var for what was really requested.

For example, a request for http://example.com/a_page will now load index.php with a_page passed to $_SERVER['REDIRECT_URL']. NOTE: this solution will push everything to index.php. You will need to include an exception like:

 RewriteCond %{REQUEST_FILENAME} !.not_me_plz.*

To allow all files that start with not_me_plz files to be served "as expected".

Sign up to request clarification or add additional context in comments.

3 Comments

What if I am trying to preserve example.com/singlepathtofile ? Imagine in the doc root my file tree may actually look like 2012/08,09,10/singlepathtofile.html ... 2013/01/singlepathtofile.html,02,03 etc.
This part 2012/08,09,10/ is very messy (hard to deal with). Why not have a URI like: http://example.com/title_of_post/2012/08,09,10 or http://example.com/2012/08,09,10/title_of_post which is very easy to deal with.
Sorry I meant to my internal doc root structure. All the requests are going through a front end controller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.