Forum Moderators: phranque

Message Too Old, No Replies

Adding index.php to ^.*/$ request

         

fish_eye

2:31 pm on Aug 6, 2005 (gmt 0)

10+ Year Member



Everytime I think I start understanding mod_rewrite I come across something that I think should be simple and it isn't.

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?

jdMorgan

3:19 pm on Aug 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> This (additional) .htaccess is stored in (the equivalent of) http://example.com/1/

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]

I don't know why you'd want to use a 302 redirect, after all the problems this has caused in Google. Consider whether you really need to do a redirect, or whether an internal rewrite would suffice.

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

fish_eye

11:38 am on Aug 7, 2005 (gmt 0)

10+ Year Member



Many thanks again Jim.....
In order to rewrite or redirect to /1/ when using code in /1/, you will need to put /1 in the substitution path

I guess this means the rule may as well just go in the main .htaccess file?

an internal rewrite

What do you mean by this please? (And this could be the solution.)

remainder of your response

I'll admit it's pretty odd.

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).

fish_eye

1:49 am on Aug 8, 2005 (gmt 0)

10+ Year Member



PS. I've posed the question of a php solution here [webmasterworld.com]