|
|
Redirecting All Htm Files To Php Site... | ||
Discussion by WeaponX with 9 Replies.
Last Update: March 17, 2011, 8:48 am | |||
My webhost has cPanel 10. something...
Thanks.
Wed Feb 8, 2006 Reply New Discussion
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.
CODE
<script type="text/javascript">
function delayer(){
document.location = "http://www.domain.com/abcd.php"
}
</script>
Replace location with your own domain+page.
In the <BODY> tag, put this code:
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.
Wed Feb 8, 2006 Reply New Discussion
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.
Wed Feb 8, 2006 Reply New Discussion
Basically, here is what mod rewrite will do:
User types in www.whatever.com/mypage1.html the server takes that request and coverts the url to www.whatever.com/mypage1.php. Then the requested php page is served.
You'll need to create or edit yout .htaccess file in your /public_html directory.
CODE
# mod_rewrite in use
RewriteEngine On
RewriteRule ^mypage1\.html$ mypage1.php
Now you could simply forward all html file requests to the index like so:
CODE
RewriteRule ^(.*)\.html$ index.php
For more advanced php style mod rewrites, here is what to do:
CODE
RewriteRule ^mypage2\.html(.*)$ index.php?act=mypage2$1
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:
CODE
RewriteRule ^/(.*)mypage2\.html(.*)$ index.php?act=mypage2§ion=$1$2
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
Thu Feb 9, 2006 Reply New Discussion
Fri Feb 10, 2006 Reply New Discussion
CODE
# mod_rewrite in use
RewriteEngine On
RewriteRule ^(.*)\.html$ $1.php
RewriteRule ^(.*)\.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:
CODE
# mod_rewrite in use
RewriteEngine On
# First we have our specific rules:
RewriteRule ^deadlink_20\.html$ index.php
RewriteRule ^deadlink_13\.html$ index.php
RewriteRule ^deadlink_1\.html$ index.php
RewriteRule ^oldlink_5\.html$ index.php
RewriteRule ^oldlink_1\.html$ index.php
# Then we have our general (default) rule:
RewriteRule ^(.*)\.html$ $1.php
RewriteRule ^(.*)\.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
Fri Feb 10, 2006 Reply New Discussion
Options -Multiviews
RewriteEngine 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.
Fri Feb 10, 2006 Reply New Discussion
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:
CODE
RedirectMatch permanent ^(.*)\.html$ $1.php
So your .htaccess file would look like this instead:
CODE
# First we have our specific rules:
RedirectMatch permanent ^deadlink_20\.html$ index.php
RedirectMatch permanent ^deadlink_13\.html$ index.php
RedirectMatch permanent ^deadlink_1\.html$ index.php
RedirectMatch permanent ^oldlink_5\.html$ index.php
RedirectMatch permanent ^oldlink_1\.html$ index.php
# Then we have our general (default) rule:
RedirectMatch permanent ^(.*)\.html$ $1.php
RedirectMatch 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:
CODE
# mod_rewrite in use
RewriteEngine On
# First we have our specific rules:
RewriteRule ^deadlink_20\.html$ index.php
RewriteRule ^deadlink_13\.html$ index.php
RewriteRule ^deadlink_1\.html$ index.php
RewriteRule ^oldlink_5\.html$ index.php
RewriteRule ^oldlink_1\.html$ index.php
# Then we have our general (default) rule:
RedirectMatch permanent ^(.*)\.html$ $1.php
RedirectMatch 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 AstaHost and Trap17.
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:
CODE
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
Sat Feb 11, 2006 Reply New Discussion
RedirectMatch 301 (.*).html$ http://bbbb.com.php
I need to exclude the directory stats - at http://bbbb.com/stats as it has html in it and the directory is locked
What code so I use to exclude only that directory form all redirects?
Thank a lot
-Alan
Tue Oct 23, 2007 Reply New Discussion
# We always leave a blank line at the bottom of the file
-reply by gfdgdg
Thu Mar 17, 2011 Reply New Discussion
Localserver Issues - Cant Chmod 777 On Some Folder i can't make some folders CHMOD 777 (2)
|
(9) How Can You Spice Up Your Basic HTML Site ? Beginner Needs Help
|
Index




