homepage Welcome to WebmasterWorld Guest from 54.211.47.170
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
How To Redirect index.php to directory
My redirects work for html extensions, but not PHP
WW_Watcher




msg:3495906
 4:30 am on Nov 4, 2007 (gmt 0)

I could use some help please, I do not want to make a mistake.

I setup my root .htaccess file to do my redirects for www non-www & index.html to / & they work great.

I have started a new project using php files starting in a directory off the root. The redirects I have setup for index.html do not work with the index.php files I have created.

Should I mess with my .htaccess in the root of the site to try to accomidate the new index.PHP files, or should I create a new .htaccess in the new working directory where the new project starts and have it work for that directory & all subdirectories.

I would prefer the second option, because I do not wish to experiment with the whole site, but I am not sure exactly how to do it. My experiments have been less than successful so far, so I come to the experts to ask.

So, The Question:
What do I need in an .htaccess file in a subfolder of the root to redirect /foldername/index.php to /foldername/ and have the same effect in all subfolders of /foldername/ so that I do not end up with duplicate file issues.

Hope This Makes Sense,
Thanks In Advance,
WW_Watcher

 

youfoundjake




msg:3496318
 12:17 am on Nov 5, 2007 (gmt 0)

I would recommend creating a new .htaccess file in your subdirectory, using the same syntax as your root but of course using the subdomain path, and adding php instead of html.
Jim should be stopping by soon to clarify my rambles, heeh.

WW_Watcher




msg:3496352
 1:27 am on Nov 5, 2007 (gmt 0)

Thanks youfoundjake,

I created a new .htaccess in the starting directory with the only changes from the main one in the root of the site being the additional path for the directory name & the change to redirect the php, instead of the html.

Works great for all redirects for that directory, but not the subdirectories of that directory (the project goes one more level deep). Will I need to create a new specific .htaccess file for each of those (over 50) directories?

Thanks!
WW_Watcher

WW_Watcher




msg:3496431
 3:31 am on Nov 5, 2007 (gmt 0)

Well it may not be pretty, but I now have all the redirects working. I created a new, unique .htaccess for each directory in the project. When I learn a better way, I can always change it, but for now, it is working, every index.php in every directory is redirecting to it's own /dirname/

IMHO, It's better to have a non-elegant solution, then no solution.

Thanks again,
WW_Watcher

Edited to add, the redirects for non-www to www are also working for the new project also.

[edited by: WW_Watcher at 3:33 am (utc) on Nov. 5, 2007]

jdMorgan




msg:3496731
 2:21 pm on Nov 5, 2007 (gmt 0)

If you are using mod_rewrite, try adding

RewriteOptions inherit

to the top-most .htaccess file that you want to use for this part of your site. That should make it unnecessary to 'repeat' the code in each subdirectory.

This assumes that your patterns in the rules themselves can handle multiple directory levels.

See Apache mod_rewrite doc for more info.

Jim

youfoundjake




msg:3497187
 11:02 pm on Nov 5, 2007 (gmt 0)

RewriteOptions inherit

Nice little feature.. no wonder i ask all my apache questions in this forum.

jdMorgan




msg:3497265
 12:57 am on Nov 6, 2007 (gmt 0)

Most hosts set it by default, but occasionally one doesn't.

Jim

WW_Watcher




msg:3497362
 3:44 am on Nov 6, 2007 (gmt 0)

Thanks JD!

I will try it out later this week, I will be adding to the new php project & I will make the change and see if it works in each new directory.

I suspect it will not work with my .htaccess in it's current fashion, as that is already the default, and the redirects of the .html versions of my index pages in other directories not in the php project do work properly using only the .htaccess in the root.

Thanks Again!
WW_Watcher

edited to add:
"This assumes that your patterns in the rules themselves can handle multiple directory levels."

I think this is what is going to choke it, and I think this is why the file I created did not work for the subdirectories.

[edited by: WW_Watcher at 3:46 am (utc) on Nov. 6, 2007]

jdMorgan




msg:3498210
 10:38 pm on Nov 6, 2007 (gmt 0)

I setup my root .htaccess file to do my redirects for www non-www & index.html to / & they work great.

This form of the index-to-/ rule doesn't support subdirectories:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://www.example.com/ [R=301,L]

while this form does:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http://www.example.com/$1 [R=301,L]

Note that any subdirectory-paths, if present, are recognized and preserved.

Jim

[edit] Corrected as noted below. [/edit]

[edited by: jdMorgan at 12:37 am (utc) on Nov. 7, 2007]

WW_Watcher




msg:3498245
 11:20 pm on Nov 6, 2007 (gmt 0)

Excellent Jim! I am taking notes! That's why I hang out here!

So, in my root .htaccess can I have both conditions and rules to handle either a index.html, or php page in any directory the site?

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http://www.example.com/ [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://www.example.com/ [R=301,L]

Or could I use the ^ in place of the extension and cover it with one condition & rule, something like

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.^\ HTTP/
RewriteRule ^(([^/]+/)*)index\.^$ http://www.example.com/ [R=301,L]

Please forgive me if it seems that I do not have a clue as to what I am doing. But that is why I am asking, I am trying to learn.

Thanks Again!
WW_Watcher

jdMorgan




msg:3498254
 11:45 pm on Nov 6, 2007 (gmt 0)

You can do it all in one:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.(html¦php)\ HTTP/
RewriteRule ^(([^/]+/)*)index\.(html¦php)$ http://www.example.com/$1 [R=301,L]

"^" means "must start with" unless it appears as the first character within "[]" defining a negative character-group. So I'm not sure what you intended with that.

Replace the broken pipe "¦" characters above with solid pipe characters before use; posting on this forum modifies the pipe characters.

For more info on regular expressions and mod_rewrite, see the documents cited in our forum charter [webmasterworld.com].

Jim

[edit] Corrected as noted below. [/edit]

[edited by: jdMorgan at 12:36 am (utc) on Nov. 7, 2007]

WW_Watcher




msg:3498262
 11:54 pm on Nov 6, 2007 (gmt 0)

I incorrectly thought it meant "anything", but now I know!

I will also go read the docs you suggested & add this code and test it out.

Thanks again Jim!

WW_Watcher

WW_Watcher




msg:3498279
 12:32 am on Nov 7, 2007 (gmt 0)

Hmmmm, I am off to read documentation, I did a little testing with the .htaccess in the project directory, I was not brave enough to do my testing at the root & I wanted to test the simplier version dealing with only php to start with.

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://www.example.com/phpprojectdirname/ [R=301,L]

In the project directory, it correctly redirected index.php to the phpprojectdirname directory, but when I moved one directory deeper (without a .htaccess), the request for index.php in that directory redirected to the phpprojectdirname directory, instead of the phpprojectdirname/dirname/ directory, it did not pass the subdirectory name into the rule.

I have to go learn more so I can understand how this works.

WW_Watcher

jdMorgan




msg:3498282
 12:37 am on Nov 7, 2007 (gmt 0)

Oops!

See the corrections posted to the code above (to prevent further copying of bad code).

Jim

WW_Watcher




msg:3498291
 12:55 am on Nov 7, 2007 (gmt 0)

Jim, it works great, we were capturing the directory structure names, but not using the $1 to pass it to the rule (See I did learn something).

I can now go and delete 50 some odd .htaccess file I no longer need(that I created yesterday)!

Thanks again Jim for your expert assistance!

WW_Watcher

Edited to add:
I am now using this from the root of the website, and it is correctly redirecting the index.html & index.php files in each subdirectory to its respective subdirectory, throughout the directory tree.

[edited by: WW_Watcher at 12:59 am (utc) on Nov. 7, 2007]

jdMorgan




msg:3498328
 2:10 am on Nov 7, 2007 (gmt 0)

Since you profess to be in learning mode, let the latter part of this thread serve as an example of what a simple typo or omission in .htaccess code can do -- Tiny little change, drastically-different behaviour! ;)

Jim

Patrick Taylor




msg:3499580
 1:12 pm on Nov 8, 2007 (gmt 0)

Sorry to be a bit thick (as well as in learning mode), but in:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/

I can see that (I think) [A-Z]{3,9}\ matches from 3 to 9 occurences of any uppercase letter, and is then followed by a space, but why do we want to do this in this RewriteCond?

Also, what is HTTP/ for at the end?

Patrick

[edited by: Patrick_Taylor at 1:13 pm (utc) on Nov. 8, 2007]

Patrick Taylor




msg:3499643
 2:44 pm on Nov 8, 2007 (gmt 0)

OK. Found the answer here -> [webmasterworld.com...]

ppeter




msg:3571842
 9:55 pm on Feb 11, 2008 (gmt 0)

I've spent hours trying to find a solution to this exact same problem for hours. Thank heavens for this thread!

Now, I'd like to take it one step further... how does one go about dealing with query strings? I tried the following without success:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php(.*)$ http://www.example.com/phpprojectdirname/$1$2 [R=301,L]

I'm also no whizz with mod_rewrite and I'm in learning mode. This seems to be one of those issues that is coming up more and more frequently though so I guess a definite solution needs to be available so we might as well push this one further!

Any help much appreciated.

Phil

g1smd




msg:3571905
 11:31 pm on Feb 11, 2008 (gmt 0)

You need:

RewriteCond {QUERY_STRING} whatever

to test the query string because it is NOT a part of the URL seen in {REQUEST_URI}.

You can also extract query-string data from {THE_REQUEST} but using {QUERY_STRING} is often much easier.

jdMorgan




msg:3571913
 11:49 pm on Feb 11, 2008 (gmt 0)

Actually, query strings are passed through unchanged, unless replaced. The only place they need to be accounted for is in the RewriteCond.

Also, an error was introduced above, and the derived solution was not a general one. The following should work with or without a query string:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\??
RewriteRule ^(([^/]+/)*)index\.php$ http://www.example.com/$1 [R=301,L]

Note that $1 contains $2, and using both $1 and $2 (as in the code in the last post above) will cause a duplicate path-part if more than one directory level is present in the requested URL-path.

Jim

[edit] Warning: This forum deletes consecutive question marks, as are required in this code! I added a work-around. [/edit]

[edited by: jdMorgan at 12:51 am (utc) on Feb. 12, 2008]

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved