Forum Moderators: phranque

Message Too Old, No Replies

Tricky .htaccess Rule Needed

make .html show .php but anyone typing .php redirects to .html - get it? :)

         

wfernley

1:51 pm on Oct 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey everyone,

I am updating my old site and all the pages are done in PHP. All the pages display .php as well as the file extension. I would like the new extension to be .html instead of .php. Is there a way I can create a .htaccess rewrite rule that will show .php pages for .html but when a user types in .php it will redirect to .html?

The reason why is the .php have all the pagerank but I am now using .html.

I hope I explained that well. :)

Thanks in advance for your help!

jdMorgan

2:11 pm on Oct 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This has been fairly well-covered here in the past, but since this is a clean thread, I'll post the solution once again.

# Redirect direct client requests for <anything>.php to <anything>.html
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*[^.]+)\.php[^\ ]*\ HTTP/
RewriteRule \.php$ http://www.example.com/%1.html [R=301,L]
#
# Serve requests for .html URLs from same-named .php files
RewriteRule ^([^.]+)\.html$ /$1.php [L]

It wasn't clear whether your real/existing files are named .php or .html. The code above assumes that the .php files exist, and you want to use a .html URL to serve them. If that's not the case, then simply reverse all instances of .php and .html.

Jim

[edited by: jdMorgan at 2:12 pm (utc) on Oct. 20, 2006]

Birdman

2:21 pm on Oct 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This thread should help you:

Do I have to change .html to .php? [webmasterworld.com]

Edit: Nevermind, I got that backwards.

I do have a question though.

Why do you need .html extension when the php pages are doing fine in the SEs?

[edited by: Birdman at 2:23 pm (utc) on Oct. 20, 2006]