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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using htaccess for mod-rewrite/php

Status
Not open for further replies.

RenoWV

Technical User
Mar 16, 2002
156
US

I had asked in the php forum how I could drop a php include directive into an existing HTML page. One of the participants suggested:

"...if you can use .htaccess include a mod-rewrite rule to redirect the "old" .html page to the "new" .php version."

The writer wasn't exactly sure what the correct mod-rewrite format would be for .htaccess, but suggested this:
Code:
RewriteEngine On
RewriteRule myPage.html myPage.php

If that's not right, could someone please suggest how this should be handled? (If possible, could you show an example of mod-rewrite for one file only -- myPage.html; and also, what is the format for mutiple files: myPage1.html ; myPage2.html ; myPage3.html etc).

Thanks for any advice...

----------------------------------------
 

Thanks Wishdiak. I did some more searching yesterday and found a website that gave this as the proper format:

Code:
RemoveHandler .html .htm
AddType application/x-httpd-php .php .htm .html
So am assuming (hoping!) that is correct...

------------------------------------
 
RenoWV,

I've got this in my httpd.conf, but I'm not certain that this would work with .htaccess.

Code:
<VirutalHost *>
.
.
.
RewriteEngine  on
RewriteRule     ^(.*)\.html$   $1.php [R=permanent]
</VirtualHost>

Wishdiak
A+, Network+, Security+, MCSA: Security 2003
 

Perhaps there is more than one way to handle this? Given that we're looking at 3 different approaches just in this thread, I think my next step should be setting up a test folder for the html & php files, then trying each of the suggested formats with htaccess.
Code:
  Version 1:
RewriteEngine On
RewriteRule myPage.html myPage.php

  Version 2:
RemoveHandler .html .htm
AddType application/x-httpd-php .php .htm .html

  Version 3:
RewriteEngine  on
RewriteRule     ^(.*)\.html$   $1.php [R=permanent]
If anyone else has insight as to which is best, please chime in! Thanks....

------------------------------------
 
RenoWV,

Maybe we should just go over the specifics of the original question.

In my case, I redirected all .html page requests to .php. The code I quoted above does this (for specific VirtualHosts).

Is your situation different? Do you want to redirect specific .htm or .html documents and not others? Do you have no access to edit your httpd.conf directly?

Wishdiak
A+, Network+, Security+, MCSA: Security 2003
 
Do you want to redirect specific .htm or .html documents and not others?
Yes, I want to minimize the impact to the server, so only want to focus on specific html files (it would not be that many).

After posting my last message I was again re-reading some of the Apache docs you recommended, but not being a server tech guy, I must be honest in saying that they are daunting in their complexity.

When I found yesterday during a web search that someone had offered a solution ( that seemed hopeful. If I can get some time free to set up my test, that is likely the first one I'll try, unless it is pointed out to be wrong....

-------------------------------------
 
RenoWV,

The instructions in the link that you quote above looks perfectly legit, however it seems to assume a few things:

1. That you are unable to modify your httpd.conf directly,
2. That you want the ability to parse all .html files for php code, and
3. That renaming the relevant files to be parsed would be inconvenient.

If all three of these are true, then removing the handler for .htm and .html and parsing all .htm and .html files (in a specific direcotry) as if they were .php files seems as good a solution as any of the others above.

Wishdiak
A+, Network+, Security+, MCSA: Security 2003
 
1. That you are unable to modify your httpd.conf directly,
2. That you want the ability to parse all .html files for php code, and
3. That renaming the relevant files to be parsed would be inconvenient.
Thank you for that clarification -- I somehow overlooked that the "solution" I found online would apply to ALL the files. DUH!

To be honest, I am not familiar with "httpd.conf" and therefore do not know whether or not our server allows modification; and I am trying to avoid renaming files, as there are lots of links in lots of places pointing to them, so that's a last option scenario.

The solution you offered is much like the original one I posted:
Code:
RewriteEngine On
RewriteRule myPage.html myPage.php
Thus, it seems THAT should be the first one I try.

Everyday I try to learn something new, and htaccess seems to be the focus this week!

------------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top