PHP mod ReWrite -> Simple Problem

Posted on 2005-03-30
Medium Priority
Last Modified: 2010-03-04
Hi everyone-

I'm a newbie, one of you will probably knock this out in 5 minutes.

I have an .htaccess file, the ReWrite engine is installed. all I want to do is have any requested .php page on my server (i.e. x.php) show up in the URL as x.html.  All the while, the actual file on the server is x.php. So really the URL is just a bogus rewrite.

I appreciate your help with this as it's driving me nuts!
Question by:cjason
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 27

Expert Comment

ID: 13669612
Mod_rewrite can only rewrite your request og xy.html internally back to xy.php. Mod_rewrite is not a markup praser, which means that you have to change all links to xy.html in your template output by yourself.
The following in a .htaccess will rewrite a request of xy.html internally to xy.php

RewriteEngine On
RewriteRule ^(.+)\.html$ /$1.php [L]

Expert Comment

ID: 13670091
Another option is to tell Apache to treat all .html pages as php using the ForceType directive in your htaccess file like:

<Files *.html>
    ForceType application/x-httpd-php

This will also require you to make sure all the links are to .html files and not to .php files. Whether this works may depend on whether you have php as a module or php as cgi.

Author Comment

ID: 13671145
Thanks caterham and TomDavidson for your feedback. I just want to be sure I'm clear on what you have explained.

1) mod_rewrite basically redirects URL requests, but does not "mask" a URL to something it is not.
2) Using an .htaccess file, I've been able to configure the server so that .html files are treated as .php, and will actually execute code.

I'm trying to figure out if this is possible:
I upload xyz.php to the server.
A user requests xyz.php, and all of the PHP code is parsed and served to the user.
Using some kind of global directive (such as in the .htaccess file), the server gives the user the xyz.php file, but the URL is displayed as xyz.html.

It seems to me that this is done on larger sites, such as CNN and ESPN, where clearly there is a lot of server-side action going on, but the end URL that the user sees is plain .html.

So just to confirm--
Is what I'm trying to do with an .htaccess file possible?


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 27

Expert Comment

ID: 13672677
> the server gives the user the xyz.php file, but the URL is displayed as xyz.html.

If you type in xyz.html and use the rewrite rule in your .htaccess, you should see the content of xyz.php (without knowing that you are on xyz.php), right.

Author Comment

ID: 13676873
That would require me to go in and change all the links that point to .php files to point to .html files. This is ok if this is the only solution, I'm just trying to figure out if there's a way to:

1) upload a php file
2) I type in a php file to load (xyz.php)
3) The php file loads, but the URL reads xyz.html

No changes would be made to any of the URLs in the HTML code of the actual pages on the server.

Is this possible using .htaccess?
LVL 27

Accepted Solution

caterham_www earned 800 total points
ID: 13680644
This is possible, but it does not make sense in therms of search engine optimazion and hiding that PHP serves the pages. You won't find your website in search engines listed, because you have no incomming links on the files you are redirecting to (xy.html).

RewriteEngine On
RewriteRule ^(.+)\.html /$1.php [L]
RewriteCond %{THE_REQUEST} \.php
RewriteRule ^(.+)\.php /$1.html [R=301,L]


Author Comment

ID: 13811699
Thanks caterham and TomDavidson for your input. It helped me figure out what I'm looking to do.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As Wikipedia explains 'robots.txt' as -- the robot exclusion standard, also known as the Robots Exclusion Protocol or robots.txt protocol, is a convention to prevent cooperating web spiders and other web robots from accessing all or part of a websit…
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month13 days, 7 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question