|
|
|
|
![]() ![]() |
Feb 8 2006, 02:36 AM
Post
#1
|
|
|
Way Out Of Control - You need a life :) Group: Members Posts: 1,086 Joined: 21-June 05 From: New York Member No.: 6,440 |
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. |
|
|
|
Feb 8 2006, 03:02 AM
Post
#2
|
|
|
PsYcheDeLiC dR3aMeR Group: Admin Posts: 2,242 Joined: 29-January 05 From: Nakorn Chaisri, Thailand Member No.: 2,411 |
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. 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. |
|
|
|
Feb 8 2006, 03:22 AM
Post
#3
|
|
|
Way Out Of Control - You need a life :) Group: Members Posts: 1,086 Joined: 21-June 05 From: New York Member No.: 6,440 |
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. |
|
|
|
Feb 9 2006, 02:59 PM
Post
#4
|
|
|
Absolute Newbie Group: Admin Posts: 871 Joined: 20-February 05 From: Indianapolis, Indiana, USA (Midwest) Member No.: 2,714 |
What you want to do is use mod rewrite.
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 |
|
|
|
Feb 10 2006, 03:34 AM
Post
#5
|
|
|
Way Out Of Control - You need a life :) Group: Members Posts: 1,086 Joined: 21-June 05 From: New York Member No.: 6,440 |
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.
|
|
|
|
Feb 10 2006, 05:17 AM
Post
#6
|
|
|
Absolute Newbie Group: Admin Posts: 871 Joined: 20-February 05 From: Indianapolis, Indiana, USA (Midwest) Member No.: 2,714 |
This should do what you want:
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 |
|
|
|
Feb 10 2006, 06:48 AM
Post
#7
|
|
|
the Q Group: [HOSTED] Posts: 980 Joined: 13-July 05 From: Lithuania, Vilnius Member No.: 7,059 |
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 -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. |
|
|
|
Feb 11 2006, 02:40 AM
Post
#8
|
|
|
Absolute Newbie Group: Admin Posts: 871 Joined: 20-February 05 From: Indianapolis, Indiana, USA (Midwest) Member No.: 2,714 |
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: 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 |
|
|
|
Oct 23 2007, 10:54 PM
Post
#9
|
|
|
Newbie [ Level 1 ] Group: Members Posts: 0 Joined: 1-November 07 Member No.: 25,869 |
On the topic above I use a
RedirectMatch 301 (.*)\.html$ http://bbbb.com$1.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 |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 5th July 2008 - 04:19 AM |