Forum Moderators: phranque
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
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
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]
RewriteOptions inherit
This assumes that your patterns in the rules themselves can handle multiple directory levels.
See Apache mod_rewrite doc for more info.
Jim
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]
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]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http://www.example.com/$1 [R=301,L]
Jim
[edit] Corrected as noted below. [/edit]
[edited by: jdMorgan at 12:37 am (utc) on Nov. 7, 2007]
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
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.(html¦php)\ HTTP/
RewriteRule ^(([^/]+/)*)index\.(html¦php)$ http://www.example.com/$1 [R=301,L]
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]
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
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]
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]
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
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]
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]