
Originally Posted by
nikomou
Hey, I know its possible to rewrite php files to html, but how would you go about that? mod_rewrite?
I, for one, am not entirely sure what you're asking for here. You mention URLs later in your post, but above you're talking about files.
The rewrite Apache module (mod_rewrite) allows a URL to be changed at request time. This may either lead to a second request from a client (a server-side redirect), or Apache might deal with the changes internally.
What exactly do you expect to happen with this change? That the server still parses a file with a .htm extension as a PHP file? Well preferably, you should avoid file extensions altogether, and use URLs like
  http://www.example.com/some-resource
You can then use content negotiation - either MultiViews or a type-map file - to respond with the appropriate resource.
By the way, the .htm extension is out-dated. It was only introduced by Microsoft because their early file systems couldn't handle extensions longer than three characters. As file names on a remote Web server have no relation to a user's file system, this is irrelevant and you should use the proper .html extension instead.
That style of manipulation - query string into file name - would need mod_rewrite, but the extension is still irrelevant.
Code:
RewriteEngine on
RewriteBase path
RewriteCond %{QUERY_STRING} ^(.+&)?network=([^&]+)(?:&(.*))?$
RewriteRule ([^.]+)\.php $1-%2.html?%1%3
This snippet could be used in a .htaccess file to do what you described. The rewrite action would be internal, so the user would think they were going to the PHP file. You'd need to replace path with an appropriate prefix. For instance, in the directory, /deep/directory/, you'd specify /deep/directory.
Code:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.+&)?network=([^&]+)(?:&(.*))?$
RewriteRule (.*/)([^.]+)\.php $1$2-%2.html?%1%3 [R]
This is a server- or virtual-host-wide rule. Unlike the previous one, this triggers an external redirect so the user would type or navigate to an address containing an .php extension, but would see an .html extension.
If you do want your server to treat .html (or .htm) files as PHP files, then you need to alter its main configuration (though you could use a .htaccess file or mod_rewrite). However, it would then treat all files with that extension as PHP files, whether they contained <?php ... ?> blocks or not (unless you started adding Directory directives to limit the scope).
Mike
Bookmarks