OutFront Forums
     Home    Register     Search      Help      Login    

Follow Us
On Facebook
On Twitter
RSS
Via Email

Recent Posts
Todays Posts
Most Active posts
Posts since last visit
My Recent Posts
Mark posts read

Sponsors
Shopping Cart Software
Ecommerce software integrated into Frontpage, Dreamweaver and Golive templates. No monthly fees and available in ASP and PHP versions.
Website Templates
We also have a wide selection of Dreamweaver, Expression Web and Frontpage templates as well as webmaster tools and CSS layouts.
Frontpage website templates
Creative Website Templates for FrontPage, Dreamweaver, Flash, SwishMax

 

.htaccess rewrite htm to php

 
View related threads: (in this forum | in all forums)

Logged in as: Guest
Users viewing this topic: none
Printable Version 

All Forums >> Web Development >> General Web Development >> .htaccess rewrite htm to php
Page: [1]
 
Starhugger

 

Posts: 631
Joined: 4/12/2005
From: Canada
Status: offline

 
.htaccess rewrite htm to php - 8/15/2009 14:37:52   
I'm redoing my site to use php-includes (instead of the old frontpage-includes). I am planning to place the following code in the .htaccess file so that search engines and linkbacks don't throw a hissyfit when pointing to my site:

RewriteEngine on
RewriteRule ^(.*)\.htm$ $1.php [nc]

My understanding is that this code will convert all requests for *.htm to *.php. As such, I'm renaming all my *.htm files to *.php (whether they use a php-include or not).

Three questions:
[1] Although I'm doing it anyway to be on the safe side, is it actually necessary to rename all *.htm files on the site to *.php, even if they don't use php code? For example, the included files that are themselves called by a php-include.

[2] Will this rewrite code affect outgoing links that access a *.htm or *.html web page? In other words, will it also try to convert those to *.php as well? If yes, is there a way to override this?

[3] Where in the .htaccess file should this code go in relation to anything else currently in the file? (I haven't even looked in it yet, but it just occurred to me that I should probably find out.)

Thanks for the help!

Starhugger
noserver

 

Posts: 1
Joined: 8/24/2009
From: USA
Status: offline

 
RE: .htaccess rewrite htm to php - 8/24/2009 1:18:16   
asking your hosting provider first are htaccess is allowed or already active or not, second is find some great thread about htaccess code

(in reply to Starhugger)
Tailslide

 

Posts: 6692
Joined: 5/10/2005
From: Out here on the raggedy edge
Status: offline

 
RE: .htaccess rewrite htm to php - 8/24/2009 6:29:20   
I'm no expert at this - always having "issues" but my understanding is that if you use PHP includes in an html page what you need to do is effectively use the .htaccess to supply .html pages as .php pages - so they'll stay as yourpage.html even though they've got php within them. You don't need to change the file extensions at all. Browsers, links, search engines etc still see whatever.html.

That's the simple bit.

The difficult bit is that different servers work differently and the addhandler you add today may not work at the next apache update.

I tend to use a single host for most of my sites. Up to fairly recently I always used .html extensions for my pages even if they had php in them. I'd just add a line to the .htaccess file to deal with that for me. No problem... until about this time last year. Apache was updated and for some reason it overwrote every single .htaccess file on all my clients' websites!! Ok - says I - no problem - just go in and re-add it. Then about a month later... apache was updated again... you can guess the rest!

I found that the best way to do this was to use Cpanel to do it - that way it "sticks". If you have Cpanel - let me know and I'll post a screenshot of what I use to make this work.

Now instead of bothering with this I'll generally do a new site just using .php extensions - it's just easier!!

I'd suggest checking with your host as to what version of apache they're using and what addhandler type to use in your .htaccess file. Last one I used was:

AddHandler application/x-httpd-php5 .php .php4 .php3 .phtml .html


But there are others...

_____________________________

Little Blue Plane Web Design | Land Rover project

:)

(in reply to noserver)
Starhugger

 

Posts: 631
Joined: 4/12/2005
From: Canada
Status: offline

 
RE: .htaccess rewrite htm to php - 8/24/2009 11:40:51   

quote:

ORIGINAL: noserver

asking your hosting provider first are htaccess is allowed or already active or not, second is find some great thread about htaccess code

Hi noserver,

Thanks for your reply. I have already checked with my webhost and actually they were the ones who recommended the code I originally posted. But I found I had more questions so I thought I'd see if anyone here knew the answers before I went back and pestered them with more questions. :)

I have searched for a "great thread" about this code but didn't find enough, so I thought I'd try to create it here instead. :)

Starhugger

(in reply to noserver)
Starhugger

 

Posts: 631
Joined: 4/12/2005
From: Canada
Status: offline

 
RE: .htaccess rewrite htm to php - 8/24/2009 18:29:25   

quote:

ORIGINAL: Tailslide

I'm no expert at this - always having "issues" but my understanding is that if you use PHP includes in an html page what you need to do is effectively use the .htaccess to supply .html pages as .php pages - so they'll stay as yourpage.html even though they've got php within them. You don't need to change the file extensions at all. Browsers, links, search engines etc still see whatever.html.

That's the simple bit.

Hi Tailslide!

Interesting... My understanding was that it went the other way around -- that I'd need to create PHP pages in order to run PHP code, but that the .htaccess could be trained to take requests for abc.htm(l) and pull up abc.php instead. What you're saying is different -- that I can run PHP code in *.htm(l) files but train .htaccess to treat them like they were *.php files.

Truth is, I've already renamed all my files to .php at this point, so I'm not sure if there would be any advantage in going back to rename them back again. If nothing else, Expression Web 2 gets upset when there's PHP code in a HTML file, so that's one reason to keep them .php for now. It doesn't seem to help with the include paths very well unfortunately, but it seems that's probably the lesser of evil options.


quote:

...Apache was updated and for some reason it overwrote every single .htaccess file on all my clients' websites!! Ok - says I - no problem - just go in and re-add it. Then about a month later... apache was updated again... you can guess the rest!

Oh no! I don't want to hear this. LOL I guess I'll have to be sure to keep a backup, whatever I do.

quote:

I found that the best way to do this was to use Cpanel to do it - that way it "sticks". If you have Cpanel - let me know and I'll post a screenshot of what I use to make this work.

Yes, I do have Cpanel. Thanks, a screenshot would help, even if I do it the other way around (which was how my host originally suggested I do it), it sounds like it would be better to do it through Cpanel if it's more "sticky."

quote:

I'd suggest checking with your host as to what version of apache they're using and what addhandler type to use in your .htaccess file. Last one I used was:

AddHandler application/x-httpd-php5 .php .php4 .php3 .phtml .html

I'll check with them and post back. Thanks very much!

Starhugger

(in reply to Tailslide)
Starhugger

 

Posts: 631
Joined: 4/12/2005
From: Canada
Status: offline

 
RE: .htaccess rewrite htm to php - 8/24/2009 21:36:12   
Hi again,

My Apache version is 2.2.11 (Unix). My Cpanel version is 11.24.4.

My webhost said it's probably better to use .php as the file extension and use the rewrite statement in the .htaccess. So I think I'm going to go with that for now, especially since I've already changed the file extensions on the web pages (in the development version anyway). It seems like it's a pain no matter which way I go. :)

If you have a screenshot of how to change the .htaccess through Cpanel, that would be great. Thanks!

SH


(in reply to Tailslide)
Tailslide

 

Posts: 6692
Joined: 5/10/2005
From: Out here on the raggedy edge
Status: offline

 
RE: .htaccess rewrite htm to php - 8/25/2009 3:44:16   
S'pose it doesn't matter really which way around you do it - shame you had to go to all that work though!!

I've attached a screenshot of the Apache Handlers screen in cpanel to show you what I use to allow .html suffixes to work on pages that have PHP in them. Not sure it would be helpful in your situation BUT I'd suggest finding which handler to use in your situation and doing it via Cpanel rather than .htaccess if possible.




Thumbnail Image
:)

Attachment (1)

_____________________________

Little Blue Plane Web Design | Land Rover project

:)

(in reply to Starhugger)
womble

 

Posts: 6009
Joined: 3/14/2005
From: Living on the edge
Status: offline

 
RE: .htaccess rewrite htm to php - 8/26/2009 13:40:53   
As Tail said, the easiest way to do it is to just use the .php extension for all your pages, which is what I've done as long as I've been using PHP, simply because I never fancied getting into playing around with .htaccess. I only used .htaccess briefly on one site which I switched from plain HTML when I first started using PHP.

I use the same host as Tail for all my sites, and the Apache upgrades have never affected any of my sites simply using the .php file extension. As you said, if you've already named your files with PHP, I'd go with that.

_____________________________

~~ "A cruel god ain't no god at all" ~~
~~ Erase hate. Practice love. ~~

(in reply to Tailslide)
BobbyDouglas

 

Posts: 5525
Joined: 5/15/2003
From: Arizona
Status: offline

 
RE: .htaccess rewrite htm to php - 8/26/2009 18:24:56   
If you go the route of actually using .php pages, and changing all of your links from .htm to .php, you will want to use .htaccess to issue a 301 redirect for all htm pages to the new php extension.

So when you have a page at www.domain.com/page1.htm it will tell the browser that your page has permanently moved to www.domain.com/page1.php

This is helpful for not just your everyday users (people who have bookmarked your pages), but Google as well. Google will index the new page since it is a permanent redirect.

I like to use FileMonkey when making large extension changes to websites, going from asp to coldfusion, htm pages to php, etc..

You can also send a ticket into your host, and have one of the admins make that change for you.

_____________________________

Arizona Web Design - Mr Bobs Web Design in Arizona
The Arizona Web Hosting Challenge

(in reply to womble)
Starhugger

 

Posts: 631
Joined: 4/12/2005
From: Canada
Status: offline

 
RE: .htaccess rewrite htm to php - 8/31/2009 1:55:57   
Thank you Tail for posting the info and the screenshot. And thanks Womble for your feedback and support. And BobbyDouglas, thanks for your feedback and the suggestion of FileMonkey. I'll try to check that out sometime this week. I've been kind of under the weather the last few days and haven't been able to do anything with this. Hopefully this week I will, after I've caught up with other things that fell by the wayside. I'll let y'all know how I make out. :)

Starhugger


(in reply to Tailslide)
womble

 

Posts: 6009
Joined: 3/14/2005
From: Living on the edge
Status: offline

 
RE: .htaccess rewrite htm to php - 8/31/2009 6:11:26   

quote:

ORIGINAL: BobbyDouglas

If you go the route of actually using .php pages, and changing all of your links from .htm to .php, you will want to use .htaccess to issue a 301 redirect for all htm pages to the new php extension.


That's a good point that Bobby made. I only ever had a problem with one site with one particular host I was with for a while. I changed all my HTML pages to PHP, but I didn't put a 301 redirect on because it was a relatively new site that had only been up a few weeks and was just getting off the ground, and figured that I didn't need to bother with the redirect. It was only some months later though that I accidentally discovered that anyone trying to get to the site by using just the domain name (e.g. www.mysite.com) would get the host's empty "waiting for content" page. Only if they specified a page as well, or www.mysite.com/index.php would they get the site's home page. It must have been something to do with how that particular host's server was set up because I've never had the same problem anywhere else, but I had to put a 301 redirect on index.html to get it to redirect to the PHP page, otherwise if they just used the domain they'd get only the holding page.

_____________________________

~~ "A cruel god ain't no god at all" ~~
~~ Erase hate. Practice love. ~~

(in reply to BobbyDouglas)
Starhugger

 

Posts: 631
Joined: 4/12/2005
From: Canada
Status: offline

 
RE: .htaccess rewrite htm to php - 8/31/2009 11:58:13   
Thank you for mentioning that, Womble. I'll have to test that when I get it up and running.

SH

(in reply to womble)
BobbyDouglas

 

Posts: 5525
Joined: 5/15/2003
From: Arizona
Status: offline

 
RE: .htaccess rewrite htm to php - 9/1/2009 0:03:12   
quote:

It was only some months later though that I accidentally discovered that anyone trying to get to the site by using just the domain name (e.g. www.mysite.com) would get the host's empty "waiting for content" page. Only if they specified a page as well, or www.mysite.com/index.php would they get the site's home page.

- Just to touch base on this one... That's the DirectoryIndex not being set correctly by your host. Unless you manually set the directory index to be index.htm or index.html, it is a problem with your host.

You can tell the server what your default file is supposed to be called for every directory. Sometimes you will see people using weird home page names, such as main.html, or home.html. Within your htaccess, if you set "DirectoryIndex index.htm index.html" it will only display the default page (when you go to a folder) if there is an index.htm or index.html file.

Most hosts have it enabled to support all the major index files, starting from most commonly used to least used.

_____________________________

Arizona Web Design - Mr Bobs Web Design in Arizona
The Arizona Web Hosting Challenge

(in reply to Starhugger)
mindcat238

 

Posts: 2
Joined: 8/19/2009
Status: offline

 
RE: .htaccess rewrite htm to php - 9/1/2009 5:53:19   
This question is too complicated for me

(in reply to BobbyDouglas)
Tailslide

 

Posts: 6692
Joined: 5/10/2005
From: Out here on the raggedy edge
Status: offline

 
RE: .htaccess rewrite htm to php - 9/1/2009 8:46:29   
Thank you for that valuable contribution!!!

_____________________________

Little Blue Plane Web Design | Land Rover project

:)

(in reply to mindcat238)
Starhugger

 

Posts: 631
Joined: 4/12/2005
From: Canada
Status: offline

 
RE: .htaccess rewrite htm to php - 9/1/2009 12:16:09   
:)


(in reply to Tailslide)
womble

 

Posts: 6009
Joined: 3/14/2005
From: Living on the edge
Status: offline

 
RE: .htaccess rewrite htm to php - 9/1/2009 14:20:03   
:)

quote:

- Just to touch base on this one... That's the DirectoryIndex not being set correctly by your host. Unless you manually set the directory index to be index.htm or index.html, it is a problem with your host.


Yep, they were crap hosts - hence I only stayed with them for about six months before I shifted the site to the reliable host I usually use for my sites. :)

_____________________________

~~ "A cruel god ain't no god at all" ~~
~~ Erase hate. Practice love. ~~

(in reply to Starhugger)
Page:   [1]

All Forums >> Web Development >> General Web Development >> .htaccess rewrite htm to php
Page: [1]
Jump to: 1





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts