Forum Moderators: phranque

Message Too Old, No Replies

Simple mod_rewrite php->html fails

need code verification

         

Storyman

7:14 am on Oct 9, 2004 (gmt 0)

10+ Year Member



It is a simple mod_rewrite (so they say) to rewrite .php as .html.

Here is the .htaccess code:

RewriteEngine on
RewriteBase /
RewriteRule ^index\.html$ index.php
[T=application/x-httpd-php]

...the code returns an error.

Somewhere I recall reading that there was a command that preceded RewriteEngine. Although not certain, I vaguely remember something like Directory.

Any suggestions as to how to fix the non-functioning code or how to find the source of the problem is greatly appreciated.

Storyman

5:32 pm on Oct 9, 2004 (gmt 0)

10+ Year Member



I've been struggling with the mod_rewrite and have gone back over the online tutorials without any success.

The mod_rewrite is definitely loaded in the website's Apache.

What I'm trying to accomplish is rather simple--so one would think.

Currently the files appear as:
[mysite.com...]

The goal is to have it rewrite it as:
[mysite.com...]

Condition:
I'd prefer to avoid rewriting the file extention stored on the server from .php to .html. The reason is that it is possible to view an html file under some conditions, which is not true with php files.

Is it possible to display files with php extensions as html files?

sabai

5:47 pm on Oct 9, 2004 (gmt 0)

10+ Year Member



RewriteRule ^index\.html$ /index\.php

You were nearly there... have to escape the . on the right hand side too..

Storyman

6:00 pm on Oct 9, 2004 (gmt 0)

10+ Year Member



Sabi,

Thanks for the feedback.

The .htaccess file now reads:
RewriteEngine on
RewriteBase /
RewriteRule ^index\.html$ /index\.php
[T=application/x-httpd-php]

I'm still getting an Internal Server Error. The Apache server has the mod_rewrite module loaded. When the final line "[T= applic...] is deleted the error goes away, but the php extension shows. Also, the "RewriteBase /" has been remarked out, but the Internal Server Error still appears.

Is there any sort of simple test to help narrow down what the source of the error code could be?

sabai

6:11 pm on Oct 9, 2004 (gmt 0)

10+ Year Member



Take a look in the apache error log...

sabai

6:15 pm on Oct 9, 2004 (gmt 0)

10+ Year Member



What are you trying to do with this line anyway?

[T=application/x-httpd-php]

Storyman

6:27 pm on Oct 9, 2004 (gmt 0)

10+ Year Member



Sabi,

I have no idea. It's included in the tutorials.

In one tutorial it is written as:
[T=application/x-http-php,L]

The alternate line has also be used without any change.

I've never seen the Apache error log and will see if I can locate it on the server.

sabai

6:34 pm on Oct 9, 2004 (gmt 0)

10+ Year Member



I think you might need that line (or something like it) if you were going the other way html->php - to tell apache to handle the file as a php script... in my config though, it is application/x-httpd-php (note the D). You don't need it and can sleep easy tonight.

Storyman

6:41 pm on Oct 9, 2004 (gmt 0)

10+ Year Member



Searched all of the folders on the website that are accessible and could not locate an Apache error log. In fact there were no error log files of any sort.

Some time ago I used .htaccess and vaguely recall using 'DirectoryIndex index' in conjunction with (I believe) AddType. Not sure if the DirectoryIndex would make any difference, but did test it with the current code--of course the result was the same.

Just to be clear. The index.php file is written as such. Some tutorials mention rewriting files, i.e. index.php as index.html.

[edited by: Storyman at 6:50 pm (utc) on Oct. 9, 2004]

sabai

6:49 pm on Oct 9, 2004 (gmt 0)

10+ Year Member



Some tutorials mention rewriting files, i.e. index.php as index.html.

That is what you are doing. I don't see what the problem is now. If you are trying to load index.html as a php file then you need to do:

AddType application/x-httpd-php .html

Storyman

6:51 pm on Oct 9, 2004 (gmt 0)

10+ Year Member



The code now reads as:

RewriteEngine on
RewriteBase /
RewriteRule ^index\.html$ /index\.php

...no internal server error is created.

The url continues to show the php extension. For whatever reason it is not rewriting the extension and I'm not sure where to go from here.

As mentioned earlier the goal is to accomplish the rewrite without changing the actual name of the file on the server. The file stored on the server should be index.php and the url address should show the file as index.html

sabai

7:02 pm on Oct 9, 2004 (gmt 0)

10+ Year Member



Storyman, the code above will make it so that if somebody types in [yoursite.com...] then the page displayed will be the output from [yoursite.com...] What do you want above and beyond that? Or is that exactly what you want, but it isn't working?

jdMorgan

7:10 pm on Oct 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The possibly-missing directive is Options +FollowSymLinks. Some servers are configured with this option already set in httpd.conf. If not, it is required to be present in .htaccess, or mod_rewrite cannot run.

Assuming that you posted the code as it actually appears in your .htaccess file, the problem is that the flags -- the text enclosed by brackets -- must appear on the same line as the RewriteRule.

Literals in regular-expressions patterns must be escaped. There is no need to escape them in the substitution string.


Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^index\.html$ index.php [T=application/x-httpd-php,L]

More information can be found in the references cited in our forum charter [webmasterworld.com].

If the above does not work, then it is possible that your server is misconfigured; Try a simpler rewrite and examine your server error log file.

Jim

Storyman

7:14 pm on Oct 9, 2004 (gmt 0)

10+ Year Member



Sabi,

Mission accomplished!

Thank you for your help. Your last posting had the operative word 'type'. This is one of those 'duh' moments. When index.html is typed the page did appear.

What I was doing was clicking the "HOME" link, which of course was to index.php. After changing the navigation bar to reflect *.html and adding the pages to the RewriteRule all of the pages successfully appear as html pages.

Thanks again.

sabai

7:15 pm on Oct 9, 2004 (gmt 0)

10+ Year Member



There is no need to escape them in the substitution string.

hey, you're right...

sabai

7:15 pm on Oct 9, 2004 (gmt 0)

10+ Year Member



Storyman: I think you need a break :-)