PDA

View Full Version : Use .htaccess to map .shtml to .html?


stevel
8-14-02, 04:44 PM
I've been through all the .htaccess tutorials I can find on the web, and they all describe the same small handful of things. I want to do something different - I am certain it can be done, but I don't understand the syntax enough to do it.

One of my sites has many pages that end with .shtml, because I was using SSI (until the hosting service I was with which I won't name but it rhymes with "hinterland" kept disabling SSI "accidentally"). So I rewrote the pages without SSI (using Dreamweaver templates - love them!) I left the file names as .shtml though.

Now I want to rename the files to .html. When I did this on another site, I wrote a program to create individual redirect pages and then, after several months, deleted the old pages. I think using a rewrite rule in .htaccess to map .shtml to .html is a better bet, but I can't quite figure out how it should be done.

Would some kind soul provide me an example? Thanks.

rainbore
8-14-02, 06:38 PM
Hi Steve,

I'm a recent refugee from Hinterland (I like it!) too.

I saved this snippet of a message from another thread, so with apologies to the true author for not being able to give proper credit...

********** snip *************
Oh, and by the way, you don't need to rename your pages to .shtml for it to work. An easier method is to add the following to your .htaccess file inside your /htdocs folder:

AddHandler server-parsed .htm
Options +Includes

That will allow all your .htm files to run SSI's. If you don't have an .htaccess file then just open Notepad, copy those two lines into the blank file, save it as .htaccess and upload it to the /htdocs folder.

********** snip *************

Good luck!

Richard L. Trethewey
www.rainbo.net

muijefr
8-14-02, 06:57 PM
I use SSI quite extensively without the .shtml sufix, just plain old .html, and I suppose this applies to .htm also, but with PowWeb and it may be an Apache requirement, the major document must be CHMOD'd to 755. I hope this has some bearing on the subject. If not, sorry.

Bradley
8-15-02, 12:32 AM
I think what you are asking is if you can redirect pages with a .shtml etension to a .html extension

Redirect permanent *.shtml *.html

I doubt it would work but you could try. Let me know if it does, if it doesn't delete it quickly b/c your whole site will give a 500 error.

Best,

stevel
8-15-02, 02:08 PM
FastLaneWeb understands what I want. Yes, I know I can use .htaccess to have SSI recognized in HTML, but I don't care, as I'm not using SSI anymore. I just want to rename the files but have people's existing saved URLs (and those in search engines) still work. I'll try the redirect idea (and will test it on a site that doesn't use shtml just to be sure...)

rainbore
8-15-02, 02:21 PM
That's easier... by which I mean I happen to have the answer, ;-)

I had the same problem in a way. A couple of years ago, I reorganized our website and moved dozens of critical pages into a new directory. Between people's bookmarks and old search engine entries, I was seeing a lot of "404 - page not found" errors. I don't know what the perfect approach is, but the simplest is a Redirect command in an .htaccess file. I'm sure there's a way to do it with a single command using wildcards if there's a pattern you can deal with, but essentially you insert:

Redirect /oldpage.shtml http://www.yoursite.com/newpage.html

for each one.

Good luck!

Richard L. Trethewey
www.rainbo.net

NMS
8-15-02, 02:45 PM
This is the link to personalise errors:

http://help.powweb.com/tutorials/htaccess/customerrors.php

This is the link to redirect:

http://help.powweb.com/tutorials/htaccess/redirect.php

stevel
8-15-02, 07:15 PM
Sigh.. All of the redirect instructions tell you to add a redirect line for every page you want to redirect. I'd rather just have a single rule that maps anything.shtml to anything.html.

The Redirect line FastLaneWeb suggests doesn't work - you get a 500 Internal Server Error.

I'd think a rewrite rule would handle it - no?

Bradley
8-15-02, 07:32 PM
I figured it wouldnt work, but it was worth a try.

The rewrite thing seems to make since but I am not sure how.

NMS
8-16-02, 05:26 AM
you have 3 important error...the 430, 404 & 500...3 lines!!

blurchy
8-16-02, 10:02 AM
hi

stevel its your lucky day :)

http://webmutant.com/changing.shtml

(applause) :)


update

oh gees wrong way round

hmm mail the webmaster of the site :)

stevel
8-16-02, 10:39 AM
blurchy,

Thanks! I think I can figure out from that page how to do it the other way. I'll try it and report my results.

blurchy
8-16-02, 11:32 AM
good :)

yes let us all know if it works out

an erm gis a copy :)

NMS
8-16-02, 01:26 PM
I have looked at that link and it is a wonderful thing with htaccess...kindly test it & modify acordingly and give me all your results so that I can create a tutorial in the help site!!

stevel
8-23-02, 07:42 PM
The comments in the original script don't, I think, describe exactly what it does in relationship to checking for file existence. The original comments says it changes foo.x to foo.y only if foo.x doesn't exist. But it really checks to see if foo.y exists and changes it only if foo.y is there. I amended the comments accordingly.

Here's my version.


# Convert .shtml requests to .html if the .html file exists
#
# Turn on the Rewrite Engine:

RewriteEngine on

# Cut ".shtml" off the end of the request, to get the basename:

RewriteRule ^(.*)\.shtml$ $1 [C,E=WasSHTML:yes]

# Change the request for "basename.shtml" to "basename.html",
# if basename.html exists

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [S=1]

# If it doesn't, then put the ".shtml" back on the end:

RewriteCond %{ENV:WasSHTML} ^yes$
RewriteRule ^(.*)$ $1.shtml

Bradley
8-23-02, 09:36 PM
COOL. I might have to give that a try If I ever try and change the page names of my site!

Glad that you found a solution!

blurchy
8-24-02, 08:35 AM
hi steve

thanks for copy of the the amended code :)

i noticed you changed the top line

i would have missed that

i did a copy yesterday but ill use yours now i know

that it actually works :)