Forum Moderators: phranque

Message Too Old, No Replies

Simple mod rewrite of html to php

Simple mod rewrite of html to php

         

Simon_Says

9:31 pm on Mar 8, 2005 (gmt 0)

10+ Year Member



I'm sure this must have a simple solution, maybe using a server variable, but can't seem to find this elsewhere either in WWorld or otherwise.

I have a folder www.mydomain.com/folder/ which has many subfolders e.g.

www.mydomain.com/folder/subfolder1/
www.mydomain.com/folder/subfolder2/
...
www.mydomain.com/folder/subfolder88/
etc

I want to place one htaccess rewrite in the www.mydomain.com/folder/ directory that rewrites an html file with a php file. So whenever anyone requests the html file (in any folder or subfolder) it actually references the exact same filename in the exact same folder but with a php extension.

I've found

RewriteEngine on
RewriteBase /folder/
RewriteRule (.*).htm$ /$1.php

But this won't work for all the subfolders and I don't understand the $1 syntax.

Thanks in advance for any help! Please put me out of my misery...

jdMorgan

12:50 am on Mar 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you really want to be "put out of your misery," spend some time with the documents cited in our forum charter [webmasterworld.com].

As for a quick answer, a simple solution is to put the following into .htaccess in your /folder:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.+)\.htm$ /folder/$1.php [L]

The $1 is a back-reference to the contents of the first parentesized subexpression in the RewriteRule pattern. You can use $1 through $9 to "copy" up to nine "pieces" of the requested local URL-path from the RewriteRule pattern. You can also use %1 through %9 to "copy" parts of the pattern matches found in the preceding RewriteCond. The capability of using regular expressions pattern matching and back-references are the major contributors to mod_rewrites power.

In this case, $1 will contain the pathname of any subfolders of /folder (if any), plus the filename, but without a filetype. By appending the .php filetype and prepending /folder/, we create the entire path to the php file.

Jim

coopster

1:59 am on Mar 9, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



To add to your misery, why not name your files with the .htm extension and allow Apache to parse those HTML files as PHP for you?

# PHP 5 
AddHandler php5-script htm
AddType text/html htm
# or, for PHP 4
# AddHandler php-script htm
# AddType text/html htm
# Or, for Apache < 2.0:
# AddType application/x-httpd-php htm

Simon_Says

9:08 am on Mar 9, 2005 (gmt 0)

10+ Year Member



Thanks both of you.

Coopster, I really need to keep the html extension for search engine reasons since there are several 1000s of pages already indexed.

Jim, I looked through the apache documentation but it didn't seem to point me towards Options +FollowSymLinks This seems to be the key. (PS the tutorial link seems broken).

I think I understand the syntax of your code but when trying your solution I get a 404 error. E.g. a request for domain/folder/subfolder/filename.html doesn't find the real file domain/folder/subfolder/filename.php

Mod rewrite is working in other parts of the site but do I need to do anything new in httpd.conf?

PS I tried prefixing the /folder/$1.php with the full server path in case I misunderstood your code but with the same result - 404.

coopster

12:30 pm on Mar 9, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'm not sure, but we have a misunderstanding.

What I am saying is go ahead and leave the filenames with the .html extension in which they were originally created and indexed by SEs. But, by using the AddHandler [httpd.apache.org] and AddType [httpd.apache.org] directives, you have told Apache to parse files with an .html extension through PHP. You can add PHP code to your .html files and the code will be processed as PHP.