Forum Moderators: phranque

Message Too Old, No Replies

Access child folder from parent

         

mattscotney

9:16 am on Mar 9, 2007 (gmt 0)

10+ Year Member



I have a number of article pages on my site and I want them displayed at the root level but stored in a child folder, is this possible.

Examples:

In browser: [mysite.com...]
Actual file location: [mysite.com...]

In browser: [mysite.com...]
Actual file location: [mysite.com...]

So in essence files in the the articles folder can be accessed via the root.

So if I type in [mysite.com...] it gets the article from the [mysite.com...] folder.

Can this be done, & if so how?

phranque

10:29 am on Mar 9, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



check out the apache url rewriting guide [httpd.apache.org].
if you only want this to happen for a subset of the resources, you can use the RewriteCond directive [httpd.apache.org] to limit the RewriteRule [httpd.apache.org].
you must be specific when asking rewrite questions about what changes and what stays the same in the url.
in your example, i might guess that you want to redirect only files that start with "article" followed by some digits and ending with ".html".
you might try something like this.
put the following in a file in your document root directory named ".htaccess":
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} ^(article[0-9][0-9]*\.html)$
RewriteRule ^.* /articles/%1 [L]

mattscotney

11:08 am on Mar 9, 2007 (gmt 0)

10+ Year Member



In one way I think we are on the right track, but on another I think I should explain it again!

In my [mysite.com...] folder I have 300+ articles each article has a unique non simular name like: pink-widgets.php, new-houses.php, fast-cars.php

In the [mysite.com...] I also have pages such as index.php, how-to-guide.php and so on.

There is no association that can distinguish the file names in the articles directory from those in the home directory.

Using the examples above I want to be able to access the pink-widgets.php, new-houses.php, fast-cars.php files which reside in the articles folder through the document root like: [mysite.com...]
[mysite.com...]
[mysite.com...]

It is sort of like having two home directories.

The idea of virtual folders of some type rings a bell...

phranque

11:41 am on Mar 9, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



ok how about this?
if a file exists in http://www.example.com/articles/, use it.
otherwise, just assume it's in http://www.example.com/

this might do it:

Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond /your/docroot/articles/%{REQUEST_FILENAME} -f
RewriteRule ^(.+) /your/docroot/articles/$1 [L]

mattscotney

12:16 pm on Mar 9, 2007 (gmt 0)

10+ Year Member



Yes that is almost it, but I need it the other way around...

If a file exists in http://www.example.com/ use it.
otherwise, look for it in http://www.example.com/articles/

I have been investigating the "search pages in more than one directory" from the URL rewriting guide. And I know it is the solution, just getting it to work the way I need that is the tricky part.

Thanks heaps.

phranque

12:55 pm on Mar 9, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^(.+) /your/docroot/articles/$1 [L]

this means if the requested file doesn't exist and it's not a directory, try redirecting to the articles subdirectory.

mattscotney

1:51 pm on Mar 9, 2007 (gmt 0)

10+ Year Member



Thanks that was exactly what I was after.

If the file.php is not in the /articles/ directory is it possible to look in another directory such as /news/?

And to integrate a 404 custom error page if the file does not exsist in the /root/ or /articles/ or /news/ directories? Because the rewrite code above kills my custom error pages! - I can see why but not how to fix it easily.

jdMorgan

4:50 pm on Mar 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll need to code a check for the requested file in each subdirectory in turn, and rewrite or redirect only IF IT EXISTS. Then if all of these fail, your custom error page will be properly invoked.

Options +FollowSymLinks -MultiViews
RewriteEngine on
#
# If files exists at requested URL, leave URL alone and skip next two rules
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.php$ - [S=2]
#
# Else check for file in /news directory
RewriteCond %{DOCUMENT_ROOT}%/news%{REQUEST_URI} -f
RewriteRule ^([^.]+\.php)$ /news/$1 [L]
#
# Else check for file in /articles directory
RewriteCond %{DOCUMENT_ROOT}%/articles%{REQUEST_URI} -f
RewriteRule ^([^.]+\.php)$ /articles/$1 [L]
#
... etc. ...

Adjust the skip count above if you add more rules, or replace [S=2] with [L] if there is no need to process additional rules when the requested .php file is found in the default directory.

Note that the rules have been restricted to acting only on .php-extension files, for the sake of efficiency and predictable operation. Also, the (efficient) pattern I used works only if there are no periods in the URL except for the one preceding the file extension. Replace "[^.]+" with ".+" if this is not the case; This will be harder/slower to match, but will allow additional periods in the URL-path.

File-exists checks and RewriteConds in general can be tricky to debug. If you have any trouble, you might want to test the RewriteCond variables by using a temporary external redirect to "print them out" in your browser address bar -- For example:


RewriteCond %{QUERY_STRING} !RwCondVars=
RewriteRule ^([^.]+\.php)$ http://example.com/$1?RwCondVars=%{DOCUMENT_ROOT}%/news%{REQUEST_URI} [QSA,R=302,L]

This simply appends a (non-functional) query string to each requested .php URL, so you can see the variables in your browser address bar and make sure they look correct -- i.e. correct path, no missing or extra slashes, etc. The RewriteCond prevents an 'infinite' loop, so that the query string is only added once and you only get one 302 redirect.

Jim

mattscotney

9:45 am on Mar 11, 2007 (gmt 0)

10+ Year Member



Thanks heaps JD, It took a bit of mucking around and a some small adjustments but finally I have managed to get it working perfectly!