Forum Moderators: phranque
Must take a certain kind of brain (like accounting - not the same type of brain but you get my drift?)
All I want to do is add 'index.php' to any requests that come to some sub-dirs.
I've had various attempts - with some weird results - most notably it seems to be stripping out one level of my directory structure!
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^.]+)/$ http://example.com/$1/index.php [R=302,L]
This (additional) .htaccess is stored in (the equivalent of) http://example.com/1/
What I get with this is:
1) a request to http://example.com/1/ winds up at http://example.com/1/ (nothing happens); and
2) a request to http://example.com/1/2/ winds up at http://example.com/1/index.php
Any clues?
The stripping out of the currect directory level is done by .htaccess *before* any pattern-matching or rewriting is done. The URL-path is "localized" to the current directory for a number of practical reasons (security being one of them). This is why .htaccess is called a "per-directory context."
Therefore, /1/ will not be present in the URL_path examined by a RewriteRule located in the /1/ subdirectory, just as "/" is not present in the URL_path examined by a RewriteRule located in the "/" (home) directory. However, it will be present if that RewriteRule is located in httpd.conf. In each case, the URL-path is "localized" by stripping the directory-specific prefix.
In order to rewrite or redirect to /1/ when using code in /1/, you will need to put /1 in the substitution path:
RewriteRule ^(.+)/$ http://example.co[b]m/1/$[/b]1/index.php [R=302,L]
You could also use DirectoryIndex to declare index.php as the default index file in those subdirectories, and do away with the rewrite code entirely.
I recommend:
1) Avoid 302 redirects unless they are truly temporary (and you know specifically when you will remove them).
2) Never use /index.anything to link to an index file. Instead use DirectoryIndex, and always link to "/".
Jim
In order to rewrite or redirect to /1/ when using code in /1/, you will need to put /1 in the substitution path
an internal rewrite
remainder of your response
The reason I'm doing it is because I have a piece or php code that generates 'breadcrumbs' based on a table containing the structure of my (sub-)site. I'm using the idea raised this thread [webmasterworld.com] but I'm using a 2D array rather than the $parent, $grandparent idea mentioned there. Something like this:
$site_structure = array (
array ('index.php', 'Blue Widgets', 0),
array ('010000_intro.php', 'Introduction', 1),...
I also use the array for "prev" and "next" pointers on a given page.
Everything now works perfectly, except when I come in on the 'root' (of this subdir).
Rather than code around it (which I can of course), I thought I'd just temporarily redirect to /index.php so the breadcrumbs work.
As mentioned - if there's some sort of rewrite that only I see (or at least that only the php version of /index.php sees) I'll be more than happy.
Cheers, Sam.
PS. I thought I might add something like this to the whole site - or to additional subdirs - so I want to make it as generic as possible. I use "/" extensively for linking.... Now I've written all this I think a more intelligent php solution is the answer (it's just a little harder - but unless the "internal rewrite" is for my eyes only I guess it's well worth the effort).