0

just wondering if there will be any problems renaming .php files .html files via .htaccess? Our existing site is html and I want to update the pages with dynamic php pages, but people have links saved to the existing html site. If this isn't possible, is my only way to create html pages with iframes to wrap the php pages in?

thanks :)

3
  • It is possible, .php file can be rewrited in .html file. But the page name people have and you rewrite using htaccess should match Commented Sep 12, 2012 at 9:59
  • 1
    mod_rewrite can do it very easily, or you could just configure Apache to let PHP handle files with a .html extension and put you PHP code in .html files Commented Sep 12, 2012 at 10:00
  • if you mean you want to redirect someone from /foo/bar.html to /foo/bar.php then yes, that will work fine. Commented Sep 12, 2012 at 10:00

2 Answers 2

3

It is not renaming the php file to html, but just redirect the file use html extension to php files.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f        # don't apply to the existing html files
RewriteRule ^([^.]+)\.html$ /$1\.php [QSA,L]  # QSA means combine query string.
Sign up to request clarification or add additional context in comments.

3 Comments

Note that . in regex is "any character" so this would also match notadocumenthtml which would lead to notadocumen.php (without the t, as that is specified by the .)
Yeah this is what I was thinking of. I just wondered if it would effect the way the PHP files run?
@GT22 No, it will not affect how the PHP files run UNLESS you use one of the following $_SERVER keys: SCRIPT_URI, SCRIPT_URL, REDIRECT_SCRIPT_URI, REDIRECT_SCRIPT_URL, REDIRECT_URL, REQUEST_URI as these will now show file.html instead of file.php (even though file.php is the actual file executed on your server)
2

i believe

RewriteEngine on
RewriteRule ^(.*)\.html $1\.php

should work for your needs (untested)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.