Forum Moderators: phranque

Message Too Old, No Replies

Is this possible?

html -> PHP -> html

         

activeco

10:09 pm on Aug 10, 2005 (gmt 0)

10+ Year Member




I would like to rewrite incoming file123.html request into the file123.php and then to return again file123.html.

Is it doable?
It would be OK even if I have to rewrite all .html requests.

jdMorgan

1:34 am on Aug 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, it's not possible without adding state information to the URL --such as a query string-- that allows the code to determine whether this is a 'first access' or 'second access.'

Jim

activeco

5:40 am on Aug 11, 2005 (gmt 0)

10+ Year Member



Thank you Jim.

There would be no second access except on the server itself. The page links to other pages/sites and has no frames.

The first part should be ok, file.htm to file.php, but I would like the output name to remain the same, file.htm in browser window.

activeco

6:55 am on Aug 11, 2005 (gmt 0)

10+ Year Member



Actually what I need is a parser.
Unfortunatelly this do not work in my .htaccess:

AddType application/x-httpd-php .php .htm .html

even adding this linr didn't help:
AddHandler application/x-httpd-php htm php

I see a lot of similar problems already discussed here.
Anyone found a solution?

jd01

10:10 am on Aug 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think if I understand the question, you are attempting to add an unnecessary step to the process...

RewriteEngine ON
RewriteRule ([^.]+)\.html /$1.php [L]

Will serve the php to equal html URL... No need to parse all html files as php, just serve the php information to the html location.

Justin

activeco

11:57 am on Aug 11, 2005 (gmt 0)

10+ Year Member



Thanks jd01,

No, a parser was exactly what I needed.
I have some old but well ranked .htm pages which should be rewritten using php.
Insted of renaming them all to .php I wanted the names to remain the same.

This did the trick:

RemoveHandler .htm
AddType application/x-httpd-php .php .htm

...but it depends on server settings, some won't allow this, reason unknown.

In this way I made only .htm files to be parsed to php, .html remained intact.

jd01

8:49 pm on Aug 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could also make the l optional on the RewriteRule for the same effect:

RewriteEngine ON
RewriteRule ([^.]+)\.html? /$1.php [L]

Justin

? = 0 or 1 of the characters or set of characters immediately preceding it.

ChadSEO

8:56 pm on Aug 11, 2005 (gmt 0)

10+ Year Member



activeco,

Be aware that if your site gets a lot of traffic, and a good portion of the pages are .htm files, this can place considerable load on the webserver. Your solution requires the PHP engine to parse through .htm files unnecessarily (in some cases), where the Rewrite solution does not. Just food for thought.

Chad

activeco

9:36 pm on Aug 11, 2005 (gmt 0)

10+ Year Member



OK guys, but would rewrite function actually execute htm files as php?
It is not only about rewriting, I needed php processing too.

Yes. I am aware of eventual extra load on server. That's why I made parsing for .htm files only.
My old files are all .htm so in the future I will make all php files with .htm extension, while regular HTML files will keep .html extension and will be not parsed to the PHP processing.

ChadSEO

9:44 pm on Aug 11, 2005 (gmt 0)

10+ Year Member



activeco,

Yes it will. When doing a rewrite like that, it changes the file that the server provides, but not the way it is provided. PHP, Coldfusion, etc., files are still processed as they would be normally. It is also transparent to the browser/user.

Chad

activeco

10:11 pm on Aug 11, 2005 (gmt 0)

10+ Year Member



Great. I already employed the parsing solution on one server but will try this on another one that didn't support parsing.
Thanks all for the excellent replies.

activeco

10:34 pm on Aug 11, 2005 (gmt 0)

10+ Year Member



Justin,

Just tried your code and it really worked as promissed. :)
The only thing with this is that I have to rename all files into .php .
Can this be somehow escaped?

jd01

10:37 pm on Aug 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you are using variables that are present in the URL's, you can rewrite to /index.php?infoyouneed=variable...

You might look in the library for a more detailed summary, or the two recent posts I made, part 1 and part 2 of mod rewrite.

Justin

activeco

7:30 pm on Aug 12, 2005 (gmt 0)

10+ Year Member



Another problem. :(

One site has a lot of rewrite rules. When I add this one from above the system goes seemingly into a loop.

What could be wrong here?:

######################################################
IndexIgnore .htaccess */.?* *~ *# */HEADER* */README* */_vti*

RewriteEngine on

#www to root
RewriteCond %{HTTP_HOST} ^www\.mysite\.com
RewriteRule ^(.*)$ [mysite.com...] [R=301,L]
#404 handling
ErrorDocument 404 [mysite.com...]
#pdf>htm
RewriteRule ^([^.]+)\.pdf$ [mysite.com...] [R=301,L]
#html>php
RewriteRule ([^.]+)\.html /$1.php [L]

jdMorgan

8:13 pm on Aug 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your last two rules create a loop.

The comment says #pdf>htm, but in fact, you are rewriting pdf->html

If the comment is wrong, then you'll need a more-advanced code method; See [webmasterworld.com...] msg#6

Jim

activeco

8:19 pm on Aug 12, 2005 (gmt 0)

10+ Year Member



Yes, I had some old .pdf files which I converted to HTML format. All requests for those files return new .html pages.

activeco

8:50 pm on Aug 12, 2005 (gmt 0)

10+ Year Member



OK, I had been removing one by one line, until found the troublemaker: it's 404 redirection.

Accidentaly, I forgot to make file1.php which should be called with file1.htm and 404 was triggered.

This is in 404red.html:

######################################################
<HTML>
<head>
<meta HTTP-EQUIV="Refresh" CONTENT="3; URL=http://mysite.com/" target="_blank">
</head>
<body><H3>We are sorry, blah blah blah</h3>
</body>
</HTML>
#####################################################

Oh boy, should I change default index extension?

activeco

10:37 pm on Aug 12, 2005 (gmt 0)

10+ Year Member



I will close this for those who can come into similar situation.

The problem was somewhere else; my home page use frames and one of the called files was not renamed into .php which sent 404 into the loop .
Finally got it straight.

jdMorgan

12:44 am on Aug 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What is the 'real' filetype of your error pages? If it is really a static html page (which I recommend, by the way, for all error pages), then just add an exception to your rule:

RewriteCond %{REQUEST_URI} !^/403\.html$
RewriteCond %{REQUEST_URI} !^/404red\.html$
RewriteCond %{REQUEST_URI} !^/410red\.html$
RewriteRule ^([^.]+)\.html /$1.php [L]

The reason I recommend simple html pages for error documents is to minimize dependencies. Otherwise, a minor error such as a 404 can cause a major problem like an infinite loop trying to serve the 404 page. Looping also slows down your server, which can then lead to even more problems. So it is best to keep error-handling primitive and dirt-simple.

Jim

activeco

7:58 am on Aug 13, 2005 (gmt 0)

10+ Year Member



I will do that too.
Thanks Jim.