Forum Moderators: phranque
In the past when I redesigned a client site I kept the old page files on the server and added some javascript to them which automatically redirected to the new pages.
This time around I'm hoping for a tidier solution. Is it possible to put these page redirections in the htaccess file and take the original pages off the server entirely? What is the best way to tackle this?
Any help is greatly appreciated.
In your .htaccess file,
RewriteEngine on
RewriteRule ([^.]+)\.htm$ /$1.php [L]
Will *rewrite* all requests for .htm files to .php, so there is no need to change your URLs or your links, the change will happen in the background and your new .php files will be served to the .htm location.
IOW Save all files as .php, upload them to your server, add the above to your .htaccess file, DO NOT CHANGE YOUR LINKS TO .php --- The information in the .php files will be delivered to the .htm extensions.
You should also be able to use one of the following in your .htaccess file to parse .htm files as .php and not have to change extensions at all:
AddType application/x-httpd-php .htm
OR
AddHandler application/x-httpd-php .php .htm
Justin
You should also be able to use one of the following in your .htaccess file to parse .htm files as .php and not have to change extensions at all:AddType application/x-httpd-php .htm
OR
AddHandler application/x-httpd-php .php .htm
If I do the above (in addition to the first bit of code you mention), does this mean I can just put the php includes right into the .htm pages and they'll work?
OR, If I chose to change the extensions of the files to .php can I delete the .htm pages from the server?
Thanks Justin!
This one tells your server to 'switch' the file served when an .htm file is requested.
Example:
When /the-file.htm is requested by a visitor /the-file.php will be served instead, but the visitor will not know where the information came from... To them it will appear as if /the-file.htm was opened and delivered to their browser.
If you decide to use one of the second two, you can add <?php ?> statements anywhere within your .htm files, because they will be 'parsed' as php.
IOW: The second two tell your server to 'parse' .htm files as if they were php files, so anything you can do in a php file can be done in your .htm files. (You do not need to use more than one of the 3 possibilities. Also, it is my understanding, which one of the second two you use depends on how php is loaded. If it is loaded as a module, you should use AddType. If it is loaded via CGI, you should use AddHandler.)
Justin
if no same names do a page by page 301 to other appropriate pages
Just to make sure the suggestions I made above are clear to all who are reading, the alternatives we are discussing eliminate the need for the 301s, by either serving the htm requests from different locations (IOW with different extensions) OR by parsing the current pages (names and extensions) as php, so the includes can be used on the existing pages (with htm extensions).
All three methods are as effective, and which is more efficient is determined by: Do all pages need to be parsed as php OR will there be current / future htm pages which will not need to be parsed as php? If there are page not needing to be parsed, (depending on the number) the mod_rewrite (solution 1) is probably more efficient. If not then I would probably opt for solution 2, for ease of use / implementation.
The other question involved is: Will all pages be individual, or will they be served via a single php file. If they will all be served via a php file, EG switching to /index.php?var=value then using mod_rewrite can still eliminate the need for the 301s by delivering the information to the correct URL from the index.php file. (This solution does not seem to apply in this case, but may for some.)
Remember, in most cases, unless there is a desired URL change to be made (you have a specific reason for changing URLs) most URL changes can be avoided via server technologies like AddHandler, AddType and/or Mod_Rewrite.
Justin
The distinction between a URL and a filename is often lost through over-simplification, and makes understanding the issue of this thread more difficult.
Jim