Click to See Complete Forum and Search --> : htaccess files


The_Magus
01-08-2007, 01:12 AM
I have used an .htaccess page to re-direct incoming requests for my "index.html" to a new file "index.shtml" as I am now using SSI for a new pop out menu.

I have been adding this menu to all my existing pages and changing the names from htm to shtml after adding the pop out menu.

I have been planning to leave the old htm files on the server as they are the ones that will have been indexed by search engines in the past.

I would like to remove the htm files without losing my listing with the search engines and wonder if I could use .htacess pages to re-direct ALL incoming requests for htm files to the new shtml files allowing me to remove all the old htm files.

I think this should be possible but wonder if I would have to put a htaccess page in all directories or is there a way I could put one in the root directory to achieve this aim.

Additionally if I have to put a file in each directory, could I use a wild card (*) in the htaccess file instead of listing all files in the directory.

Cheers

Jack
The Magus

Fang
01-08-2007, 01:46 AM
http://httpd.apache.org/docs/1.3/howto/ssi.html#configuringyourservertopermitssi

The_Magus
01-08-2007, 04:03 AM
Dear Fang:

I think you misunderstood my post as I have solved the problems associated with SSI however as I have renamed all my files to "shtml" I want to get rid of all the old "htm" files but am worried that all the search engines which have indexed these old files will be sending viewers to these old files resulting in a 404 page.

I am now looking for a way to include something like an htaccess file to send all the viewers who come looking for the htm files to my new shtml files. I did this for the main index.html file but now want to do something similar with the old files.

Thanks for your interest. I will be reviewing the suggested page in any case as I might learn something further about SSI.

Cheers

Jack
The Magus

TheBearMay
01-08-2007, 07:09 AM
Take a look at mod_rewrite: http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html

LeeU
01-08-2007, 08:36 AM
I used this to change ".htm" files to ".php" and it works fine. You would just need to change the ".php" to ".shtml". From the "URL Writing Guide (http://httpd.apache.org/docs/2.2/misc/rewriteguide.html)"


Backward Compatibility for YYYY to XXXX migration

Description:

How can we make URLs backward compatible (still existing virtually) after migrating document.YYYY to document.XXXX, e.g. after translating a bunch of .html files to .phtml?
Solution:

We just rewrite the name to its basename and test for existence of the new extension. If it exists, we take that name, else we rewrite the URL to its original state.

# backward compatibility ruleset for
# rewriting document.html to document.phtml
# when and only when document.phtml exists
# but no longer document.html
RewriteEngine on
RewriteBase /~quux/
# parse out basename, but remember the fact
RewriteRule ^(.*)\.html$ $1 [C,E=WasHTML:yes]
# rewrite to document.phtml if exists
RewriteCond %{REQUEST_FILENAME}.phtml -f
RewriteRule ^(.*)$ $1.phtml [S=1]
# else reverse the previous basename cutout
RewriteCond %{ENV:WasHTML} ^yes$
RewriteRule ^(.*)$ $1.html

The_Magus
01-08-2007, 02:19 PM
Dear LeeU:

One thing I like about what I am doing is that I will never suffer from a big head because as soon as I figure out something which is apparently simple for most people I am confronted with something such as the material to which your link presented.

There is no chance that I will ever understand all that material although I will give it a try as time permits but in the meantime I will go on trying to find an answer about .htaccess files as I actually got the first one to change the extension for the incoming requests for my home page from html to shtml.

Having smelled a bit of success I think I can master it again in a slightly different way.

Thanks for your interest in any case.

Cheers

Jack

LeeU
01-08-2007, 02:36 PM
I understand what you're saying but you don't need to understand each step. Basically, when the browser requests "yourfile.htm", this code in your .htaccess file will cause your server to change the extension to ".shtml" and look for the file "yourfile.shtml". If not found, it will then look for the original, "yourfile.htm". Isn't that what you wanted? I recently changed by site from ".htm" to ".php" and I use this and it works fine.

Here is the code itself.

RewriteEngine on
RewriteBase /~quux/
RewriteRule ^(.*)\.htm$ $1 [C,E=WasHTML:yes]
RewriteCond %{REQUEST_FILENAME}.shtml -f
RewriteRule ^(.*)$ $1.shtml [S=1]
RewriteCond %{ENV:WasHTML} ^yes$
RewriteRule ^(.*)$ $1.htm

The_Magus
01-09-2007, 01:07 AM
Dear LeeU:

If I understand correctly, you suggest I include the code you have provided in an .htaccess file.

I read someplace that all the code should be on one line although that may have been applicable only to the code needed to change my home page (index.html) only and not applicable to .htaccess files in general.

Where should this file be located - in the root directory or in all folders?

It would be great if I could get something like to work.

Cheers

Jack
The Magus

LeeU
01-09-2007, 08:26 AM
Yes, place that code in an .htaccess file, placed in the root of your Web directory (be sure to upload it in ascii format). Don't place them all on one line, just leave them as they are written and it should work just fine. You can then test it your self. Say you have a file "test.htm" and are now using "test.shtml". If you place the URL to the file "test.htm" in your browser, you should be redirected to "test.shtml" w/o you even knowing it.

The_Magus
01-10-2007, 02:55 AM
Dear LeeU:

Am afraid I've got a blip in the system someplace.

I copied and pasted your code into my .htaccess file as instructed and uploaded it only to get the following message when I tried to access an htm file:

The requested URL /~quux/Yachts_for_sale/index_sailing_yachts.shtml was not found on this server.

However as soon as I changed the extension from htm to shtml I got the new page indicating the above is incorrect - the file was there.

I note that the returned error page indicates it had looked for an shtml page so the .htaccess file is being read but as said there is a blip someplace.

I have uploaded the original .htaccess file in the meantime.

Any suggestions as to where I go next????

Cheers

Jack
The Magus

bathurst_guy
01-10-2007, 04:46 AM
The requested URL /~quux/Yachts_for_sale/index_sailing_yachts.shtml was not found on this server.

You might want to change it to match your server rather than Lees.

LeeU
01-10-2007, 08:50 AM
Yes, that was my fault. If this is in your root directory, then it should be:

RewriteBase /

Sorry about that.

The_Magus
01-10-2007, 02:40 PM
Dear LeeU:

Without being over the top, you are a prince among men!.

Have just tried the code with the change suggested and it works a treat.

If there is an shtml page, it produces it but if not it produces the htm page. I tested it on my index page which is still an htm page as I did not want to go "live" so have not yet uploaded the index.shtml page but it looked for the shtml but could not find one so produced the htm page. In the other cases I followed some URLs from Google which showed an htm extension but got the shtml page as hoped for.

The funny thing is that it produces the shtml page but the address box at the top of the browser indicates it is producing an htm page - not that I can see that this matters in the least.

Needless to say, I am very pleased with your advise and assistance.

Thank you very much.

Jack
The Magus

LeeU
01-10-2007, 02:43 PM
Thanks but I can't take the credit. It's right out of the Apache rewrite manual. Glad it worked!

The_Magus
01-11-2007, 01:16 AM
Dear LeeU:

Just one final note on this subject.

I had previously found or been pointed to a bit of code to change the home page from html to shtml which I put in an .htaccess file.

I initially uploaded your suggested code in an .htaccess file without the above bit of code as I was concerned it might screw up the action of your code however this morning I have added the code to change the home page to shtml in the same .htaccess file as yours and am pleased to report that it all works the way I was hoping.

You might not be able to claim credit for the code but you certainly are due credit for your interest and for taking the time to provide the code in a way that I could understand what I had to do which resulted in success.

Ever onward and upward.

Jack
The Magus

LeeU
01-11-2007, 08:48 AM
Great, Jack. I'm glad all worked out for the best.

The_Magus
01-12-2007, 09:29 AM
Dear Lee U:

I would like to prevail upon you once again for one final bit on this subject.

I have discovered that I have some old files which used the extension "html" in addition to the "htm" files which I was initially concerned about.

Is it possible to include html files in the code you posted so that incoming requests for either type would action the new smhtl file. If so could you
assist once more.

Cheers

Jack

LeeU
01-12-2007, 09:49 AM
Well, in lieu of something better, you could just try adding this to what you already have in your .htaccess file and see if it works:

RewriteBase /
RewriteRule ^(.*)\.html$ $1 [C,E=WasHTML:yes]
RewriteCond %{REQUEST_FILENAME}.shtml -f
RewriteRule ^(.*)$ $1.shtml [S=1]
RewriteCond %{ENV:WasHTML} ^yes$
RewriteRule ^(.*)$ $1.html

I don't see why it wouldn't work. There may be a way to include it all together with the other one but I am not sure. I am checking on it, however and will let you know as I am curious also.

The_Magus
01-13-2007, 10:01 AM
Dear LeeU:

I tried adding your latest code to the .htaccess file and created problems with the original index (home) page which is also an html file.

The code I had in the .htaccess file which sorted it out "Redirect /index.html/ http://www.allaboutmarmaris.com/index.sthml" worked well when I ran a search for my home page and then tried to access it i.e it produced the new page with an shtml extension but once I put your code in it did not want to do this and tried to produce the old page.

I am sorry but I am involved in another project today and will get back to working on this one later - fortunately there are only a few of the html files but I did want to sort so they would produce the new shtml page if it was not too much bother.

Guess I can live with the minor problem until I have time to get back to it.

Cheers

Jack
The Magus

LeeU
01-16-2007, 09:10 AM
Jack, see if this works:

RewriteBase /
RewriteRule ^(.*)\.(htm|html)$ $1 [C,E=WasHTML:$2]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [S=1,R]
RewriteCond %{ENV:WasHTML} ^htm$
RewriteRule ^(.*)$ $1.htm
RewriteCond %{ENV:WasHTML} ^html$
RewriteRule ^(.*)$ $1.html

The_Magus
01-17-2007, 11:14 AM
Dear LeeU:

Sorry for delay in getting back to this but have been very busy.

I tried changing the content of the .htaccess file as suggested and that seemed to cause more problem i.e it seemed to overide the rewrite I originally had to sort out the index.html file and produced a page could not be found page.

Additionally it would not produce any of the htm files - again page not found.

I have just checked my site listing in Google and notice that they have now indexed a lot of my new shtml files - in the last 24 hours.

I went through and tried to find some the html files I was worrying about and could not find any - they seem to have been updated with the new shtml file which seems to resolve the problem I was worried about. I don't know about other search engines but have decided to leave well enough alone and get on with up dating the several hundred files which have not been changed to shtml.

I really appreciate the effort you have expended on my behalf and thank you very much. Will never be able to do the same for you but you can relax knowing you have my best wishes for a long and healthy life.

Thanks again.

Jack
The Magus

LeeU
01-17-2007, 03:23 PM
Thanks, glad I could help.