Forum Moderators: phranque
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...
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.+)\.htm$ /folder/$1.php [L]
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
# 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
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.
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.