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.

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:
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
Reply