Jump to content
xisto Community
WeaponX

Redirecting All Htm Files To Php Site...

Recommended Posts

Hi, just finished converting all my files from .HTM to .PHP files. Is there an easy way to redirect all my users who go to the .HTM pages to the .PHP pages instead? Basically all the names remained the same. Just the extension changed.My webhost has cPanel 10. something...Thanks.

Share this post


Link to post
Share on other sites

Include a short redirection code in your HTM file --> PHP file.

Here's a tiny redirection script that in conjuction with the setTimeOut command waits for that many seconds before redirecting to the new page.Include this in the HEAD of your page.

<script type="text/javascript">	function delayer(){  document.location = "http://forums.xisto.com/no_longer_exists/;	}</script>

Replace location with your own domain+page.

 

In the <BODY> tag, put this code:

<body onload="setTimeout('delayer()', xxxx)">

This will cause the setTimeout function to wait for xxxx milliseconds and then call the delayer function, which in turn loads your new page. Or you could bypass the setTimeout alltogether - and use the delayer function rightaway with onload. Will work all the same.

Share this post


Link to post
Share on other sites

Thanks m^e,Just reread what I put in the title and message...I should have been more clear. Sorry...I don't have the .HTM files anymore but if someone goes to one of them, it's a Page not found message. I want a way to redirect them to the .PHP page if this can be done.

Share this post


Link to post
Share on other sites

What you want to do is use mod rewrite.

 

Basically, here is what mod rewrite will do:

User types in http://forums.xisto.com/no_longer_exists/ the server takes that request and coverts the url to http://forums.xisto.com/no_longer_exists/. Then the requested php page is served.

 

You'll need to create or edit yout .htaccess file in your /public_html directory.

 

#  mod_rewrite in useRewriteEngine OnRewriteRule ^mypage1\.html$ mypage1.php

Now you could simply forward all html file requests to the index like so:

RewriteRule ^(.*)\.html$ index.php

For more advanced php style mod rewrites, here is what to do:

RewriteRule ^mypage2\.html(.*)$ index.php?act=mypage2$1
/mypage2.html?&section=section1 would point to /index.php?act=mypage2&section=section1

 

Now you may notice the (.*) right after the .html, this means everything at that location of the url. So if you were doing a lot of mod rewrites and you missed something, your link wouldn't be broken. Whatever in located in the position of the (.*) will be inserted at the end where you see the $1. You can place a more specific rule in the () if you prefer. You can also use the command multiple times like so:

RewriteRule ^/(.*)mypage2\.html(.*)$ index.php?act=mypage2&section=$1$2
/section1_mypage2.html would point to /index.php?act=mypage2&section=section1

 

I think you'll need to let us know what rules you need help with. I don't know what your drirectory looked like before or after your changeover.

 

I hope this helps. :)

 

vujsa

Share this post


Link to post
Share on other sites

Thanks vujsa...for that script though, is there any way to redirect it to the same/respective PHP page? I don't want all of them to go to index.php but to go to their own PHP file.

Share this post


Link to post
Share on other sites

This should do what you want:

#  mod_rewrite in useRewriteEngine OnRewriteRule ^(.*)\.html$ $1.phpRewriteRule ^(.*)\.htm$ $1.php
It'll take any file.html and return file.php instead. No matter what the name of the HTML file is, the server will return the equivalent PHP file. Note that I have a rule here fore both .html and .htm, the .html rule should com first since .html contains .htm. If you put the .htm first, then the server could change your file.html to file.phpl. See how that works? If you don't understand that part be sure to ask, it is very important. The rewrite engine picks the first match even if there is a better rule after it.

 

If you have other rewrite rules to include, be sure to put them above this rule since this rule is the most general. It is usually used as the default setting.. If the file doesn't match any specific rule, then just change the file extention to .php from .html.

 

I would also check your error logs and find out which links may be dead on your site. Pages removed, pages renamed, etc... Then write specific rules for your dead links to be rewritten as index.php. The specific rules should always come before the general rule.

 

So here would be a better .htaccess file for you:

#  mod_rewrite in useRewriteEngine On# First we have our specific rules:RewriteRule ^deadlink_20\.html$ index.phpRewriteRule ^deadlink_13\.html$ index.phpRewriteRule ^deadlink_1\.html$ index.phpRewriteRule ^oldlink_5\.html$ index.phpRewriteRule ^oldlink_1\.html$ index.php# Then we have our general (default) rule:RewriteRule ^(.*)\.html$ $1.phpRewriteRule ^(.*)\.htm$ $1.php# We always leave a blank line at the bottom of the file.
You'll have to replace the "deadlinks" above with your own information. Notice that the deadlink and oldlink files are in reverse numeric order to prevent deadlink_13 being rewitten for the rule for deadlink_1. It is that same issue as above where rewrite looks for the first match, not the best match!

 

You should leave a blank line at the bottom of the file so that cPanel has a place to insert commands if need be.

 

I don't remember for sure if this rule will effect all subdirectories or not so you'll need to do some testing and checking.

 

Good Luck. :)

 

vujsa

Share this post


Link to post
Share on other sites

be careful using .htaccess files, because using them, googlebot might get an 406 error page, you would not want that right ? do like vujsa told you, but keep in mind to set:Options -MultiviewsRewriteEngine On.....Because it seems that, I can't remember on what apache version, using multiviews, googlebot gets an 406 error and you might not get indexed in google, that is one of the worst things to happen for a website. :)

Share this post


Link to post
Share on other sites

I've given this some thought and decided that you could also use url redirects in your .htaccess instead of mod rewrite:

The upside to mod rewrite is that the user never knows that the page has moved. the page still appears to be whatever.html!

But that means that you could end up generating more and more links to the .html pages by users bookmarking the page with the outdated url.

If you use a redirect rule instead, you could then display the actual new url. It works much the same way as modrewite:

RedirectMatch permanent ^(.*)\.html$ $1.php

So your .htaccess file would look like this instead:
# First we have our specific rules:RedirectMatch permanent ^deadlink_20\.html$ index.phpRedirectMatch permanent ^deadlink_13\.html$ index.phpRedirectMatch permanent ^deadlink_1\.html$ index.phpRedirectMatch permanent ^oldlink_5\.html$ index.phpRedirectMatch permanent ^oldlink_1\.html$ index.php# Then we have our general (default) rule:RedirectMatch permanent ^(.*)\.html$ $1.phpRedirectMatch permanent ^(.*)\.htm$ $1.php# We always leave a blank line at the bottom of the file.

This may be a better solution for you since it would eliminate the chance of outdated urls being bookmarked or indexed.

Alternatively, you could use a combination of the two. Have the converted pages redirect to their new versions and have dead links rewrite to index.php:
#  mod_rewrite in useRewriteEngine On# First we have our specific rules:RewriteRule ^deadlink_20\.html$ index.phpRewriteRule ^deadlink_13\.html$ index.phpRewriteRule ^deadlink_1\.html$ index.phpRewriteRule ^oldlink_5\.html$ index.phpRewriteRule ^oldlink_1\.html$ index.php# Then we have our general (default) rule:RedirectMatch permanent ^(.*)\.html$ $1.phpRedirectMatch permanent ^(.*)\.htm$ $1.php# We always leave a blank line at the bottom of the file.
Note that I left the order the same to prevent a url being redirected before it could have been rewritten.

This would cover most of your situations and either reduce the number of requests on obsolete urls or hide the fact that some pages no longer exist by serving up your index.php instead.

I haven't had any trouble with Google indexing my site by using mod rewrite. Infact, I use mod rewrite to make it easier for the robots to index my site. Some of them get confussed by the long php query urls (index.php?action=whatever&location=whereever).

Keep in mind that using .htaccess files carries with it no dangers. The dangers come from the commands that you place in it. A bad mod rewrite rule will cripple your website. In fact while doing some testing to help answer this question, I accidentally disabled the Free Web Hosting Application Form for both Xisto and Xisto. :P I rewrote all of my root directory PHP files as HTML files. Since I don't actually have a req_form.html file, a few members got a "404 - Page Not Found Error".

A general redirect rule like I have shown you would be just as debilitating to my website. Be sure to add rules to offset a general rule if you need to exclude something. I believe that rules are only run once on a n incoming url so if you set specific rule to redirect or rewrite to itself first, you would effectively exclude that url from the change.

Say for instance you have an HTML file that you don't want to convert and you wanted to exclude it from the rules above:
RewriteRule ^myHTMLpage\.html$ myHTMLpage.html
Since this is a very very specific rule, you should place it at the top of the file.

============================================

There is another option to all of this, we could write a PHP script that would check and see if the requested PHP file exists. If the file does exist the script will serve that file otherwise the index.php file will be served.

This would consist of a mod rewrite rule to change the .html url into a .php query url to the redirection script. The redirection script (maybe something like /redirect.php?file=mypage) would then use the file name as the basis of a check to see if a .php file of that name exists. If that file does exist, redirect to that file. If not, redirect to index.php. I would be happy to discuss this further in the apropriate forum if you are interested.

I hope this helps. :)

vujsa

Share this post


Link to post
Share on other sites

# mod_rewrite in useRewriteEngine On# First we have our specific rules:RewriteRule ^deadlink_20.Html$ index.PhpRewriteRule ^deadlink_13.Html$ index.PhpRewriteRule ^deadlink_1.Html$ index.PhpRewriteRule ^oldlink_5.Html$ index.PhpRewriteRule ^oldlink_1.Html$ index.Php# Then we have our general (default) rule:RedirectMatch permanent ^(.*).Html$ $1.PhpRedirectMatch permanent ^(.*).Htm$ $1.Php

# We always leave a blank line at the bottom of the file

 

-reply by gfdgdg

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.