Forum Moderators: phranque
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?
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} ^(article[0-9][0-9]*\.html)$
RewriteRule ^.* /articles/%1 [L]
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...
this might do it:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond /your/docroot/articles/%{REQUEST_FILENAME} -f
RewriteRule ^(.+) /your/docroot/articles/$1 [L]
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.
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.
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. ...
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]
Jim