Forum Moderators: phranque

Message Too Old, No Replies

1-page Mod Rewrite

From php to html

         

patrick89

3:31 am on Aug 29, 2008 (gmt 0)

10+ Year Member



Hi,

I have a contact page with the following extension: "/contact.html"
...and would like to replace it with a dynamic "/contact.php" page.
(up to this point, my entire site was in .html)

However, the original .html URL is bookmarked and used quite heavily by our clients.

With mod rewrite, is there a way to change the .php version to output .html yet still maintain the dynamic functionality?

Thanks,
Patrick

wheelie34

11:53 am on Aug 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Patrick

There's 2 ways of doing it, first is make the html page function as a php page thus keeping the extension as html but allowing php to run within the page, second, 301 redirect all calls to the new page, I would go with the first method as the page is 'heavily bookmarked'

HTH

jdMorgan

1:14 pm on Aug 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Minor correction:

> There's 2 ways of doing it, first is make the html page function as a php page thus keeping the extension as html but allowing php to run within the page, second, internally rewrite all calls to the new page. I would go with the first method as the page is 'heavily bookmarked'

You can parse html pages for PHP by using AddHandler or AddType -- telling the sever to 'inspect' html files for embedded PHP code.

Alternately, you can use a mod_rewrite RewriteRule to tell the server, "When someone asks for the URL contact.html, serve them the contents of the contacts.php file instead." This is an internal rewrite, and is not visible to the client, i.e. the browser address bar will still show "contact.html".

Jim

patrick89

3:30 pm on Aug 29, 2008 (gmt 0)

10+ Year Member



Jim,

Thank you for the help. If it's not too time consuming, could you possibly post the code for each method?

Thanks again,
Patrick

jdMorgan

4:28 pm on Aug 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



We don't provide a code-writing service here, as that would simply take too much time for the very-limited number of volunteer contributors we have -- Demand would far outstrip supply, and we all have our day jobs to do. Rather, our purpose is to help you learn to write your own code; Please review our Forum Charter, check out the threads in our forum library, and try searching WebmasterWorld for previous threads where this subject is well-covered. Links to these resources are at the top of almost every page at WebmasterWorld.

Learning the syntax differences between an external redirect and an internal rewrite is trivial -- it's in the documentation. It is understanding the functional differences [google.com] that is critical.

Jim

acimag

6:28 pm on Aug 29, 2008 (gmt 0)

10+ Year Member



All you have to do is add this sniplet into your HTACCESS FILE

RemoveHandler .html .htm .file .inc
AddType application/x-httpd-php .php .htm .html .file

If your .php file works now..

Just RENAME the PHP file to .html
and it will work fine. NO PROBLEMS no need to add anything else. It will sync and you dont hvae to do another thing at all! EVER!

hope that helps. Ive gotten a lot of help here so im glad to pitch in!

patrick89

7:14 pm on Aug 29, 2008 (gmt 0)

10+ Year Member



Jim/acimag,

Thanks for your help, much appreciated.

acimag, I also use SSI to include pages in my HTML, so, unfortunately, it appears using that code will disable the SSI. Also received this from my webhost when I asked them to help troubleshoot:

"In fact these two rules are conflicting. You are using 'Server Side Includes' for html pages and also want to parse them as PHP pages at the same time which is I am afraid not possible. We have to specify the rules in .htaccess and the rule which will come later will override the other one. So either the .html pages can be parsed as SSI or as PHP."

BTW, using just "AddType application/x-httpd-php .php .htm .html .file " results in the browser attempting to download the file.

So, I guess I'm left with the mod rewrite option. Performed a search and tried the following, but apparently it just went into a loop:

RewriteRule ^(.*)\.php $1.html [R=301,L]
RewriteRule ^(.*)\.html $1.php [L]

Just in case, also in my htaccess was:
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*)index\.html\ HTTP/
RewriteRule index\.html$ http://www.example.com/%1 [R=301,L]
#
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

(both to remedy canonicalization)

Tons of thanks for the help thus far.

[edited by: jdMorgan at 2:01 am (utc) on Aug. 30, 2008]
[edit reason] example.com [/edit]

jdMorgan

7:36 pm on Aug 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please post all the rules as one single code block, so we can see their order.

Other than that, you will have to use a RewriteCond examining THE_REQUEST on your new 301-redirect rule, just as in your older canonicalization rules, in order to prevent the loop.

Jim