Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Redirect call for index.htm to index.php? 1

Status
Not open for further replies.

theniteowl

Programmer
May 24, 2005
1,975
US
I have created a new web site to replace a very old version for a local school. The link to the site from the other school sites currently points to index.html. I need to redirect those requests to index.php which kicks off my templated system.

Is the best way to do this with an htaccess file?
Anyone have the commands needed to do this?
I would like the redirect NOT to apply to sub folders, only to requests in the main folder.

Thanks.

At my age I still learn something new every day, but I forget two others.
 
Hi

As it is probably a global change appliable to all directories, would be better to put it in the global configuration file ( httpd.conf ) instead of per directory config files ( .htaccess ).
Code:
RedirectMatch permanent (.*)/index.html$ $1/index.php

[gray]# or[/gray]

RewriteRule (.*)/index.html$ $1/index.php [R=303]
But I do not understand why anyone should link explicitely to your index file, instead of just to the directory. I would write a 404 error script to display the error message and inform the visitor that the site structure changed.

Feherke.
 
Other school sites in the district have links pointing to the old main page which uses index.html. Their paths are hardcoded to the actual file rather than the folder.
The links are on several different school sites administered by different people and I cannot rely on all changes occurring at the same time when we are ready to cut over to the new site so I figured it would be best to redirect any incorrect links. I do not want the links to end up broken as that will only affect the visitors. I can follow up with the admins of the other sites at a later time.

I am not very familiar with Apache and have not used the httpd.conf file. Is this something an admin on the server would have to change? I have FTP access to do updates to this one site but I am not listed as an official contact with the hosting service so cannot get them to make changes for me without jumping through a lot of hoops and routing the request through several other individuals who have no knowledge of the pages or the environment so things get messy. So I try to do everything possible without having to involve changes at the administrative level.



At my age I still learn something new every day, but I forget two others.
 
Hi

httpd.conf and .htaccess has similar structure and role, just the first applies globally, while the second locally for a directory. Of course some directives can be use only globally and of course httpd.conf is accessible only for a restricted number of users.

If you have only FTP access, then can forget the httpd.conf. Usually is not accessible through FTP.

The reason while I suggested to use httpd.conf instead of .htaccess, is that httpd.conf is read and parsed only once at start, while .htaccess is checked for changes at each request.

Try to put one of the directives I wrote previously into the .htaccess file. The first needs [tt]mod_alias[/tt], and the second [tt]mod_rewrite[/tt].

Feherke.
 
I will give it a try when I get home tonight.
I have a copy of the new site up on my own server for development and testing, I just do not have remote access to the server from work as they have blocked all the ports and I have not spent the time to find a way around it yet. :)

About htaccess files. I notice that when I FTP an htaccess file out to the server I can see it as long as that FTP session is open but when I go back another time the files are not visible to me. I know they are there because they are still functioning.
How can I access the files I know to be there without direct access to the server? I can overwrite the files by copying new versions there but occasionally I need to look at one to see what version it was or what it's purpose is and do not want to overwrite it without looking it over.

And while on the subject of htaccess files, I would be tremendously grateful if you have a solution to my current dilema. I have posted on it before but received no responses.

I built a templated web system using htaccess and PHP.
My htaccess file is thus:
Code:
RewriteEngine on
RewriteRule \.(html|htm|php|php4|php5)$ /jfk/jfk2/proc.php?url=%{REQUEST_URI}&%{QUERY_STRING} [R]

I currently have the same htaccess file in every sub folder of jfk2. JFK is the root of the site and jfk2 is just a sub that I use while developing and testing. Eventually everything will migrate into the root.

I use the RewriteRule to intercept every page request and redirect it to a processing script passing the original URL and any query string parameters on the new URL.
The proc.php script parses out the request and any parameters then redirects to my index.php page passing in the values that tell the template which page was requested so it can load that page into the template and set the correct options for the navigation menu.

The problem currently is that I have to keep a separate copy of the htaccess file shown above in every sub folder off the root of the web site in order to capture the requests. I would like to have the htaccess file just exist in the root but think it might interfere with calls to the important files like index.php and proc.php.

I know very little about htaccess and have through lots of examples, trial and error come up with what I have above.
I have been looking for a way to tell the htaccess file to ignore requests going directly to the root but trap all calls to sub-folders.

Perhaps the total solution is one htaccess file in the root that tests if the request is to the root and directs it to index.php and any sub-folder requests do the rewrite to proc.php.

I am somewhat lost with the htaccess portion. It will take a lot more time reading/learning than I have available right now but I am trying to get the school's new site up and running.

I know I could make a sub-folder off the root and place all other sub-folders under that one with the htaccess file there as well but then that is something unusual for anyone maintaining the pages to have to remember and if they put their files off the root it breaks them out of the template.

Thanks for the help. I will play around with the above commands this evening.


At my age I still learn something new every day, but I forget two others.
 
feherke, the RedirectMatch line worked perfectly.
I tried adding my own RewriteRule to the htaccess file in the root folder also to see if I could get both to work but without success. I would either get failures loading the page or I would get caught in a loop of redirection.
Any thoughts how I can keep it all in one htaccess file in the root folder so I do not have to have seperate files in every subfolder?

Thanks for the help.


At my age I still learn something new every day, but I forget two others.
 
Hi

.htaccess' settings applies to subdirectories. And of course this may lead to infinite loops.

In the codes I suggested the directory is kept and the file extension changes, so will not match in a second step.

How your code behaves, no idea. Post it.

Feherke.
 
Code:
RewriteEngine on
RewriteRule \.(html|htm|php|php4|php5)$ /jfk/jfk2/proc.php?url=%{REQUEST_URI}&%{QUERY_STRING} [R]

I am trying to determine how to alter this so it only affects files BELOW the current folder but have not had much success.



At my age I still learn something new every day, but I forget two others.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top