.htaccess

Hi folks,

I kind of wonder if anyone can advise or point me in the right direction:

My host system is Debian 8.4, running Virtualbox 5.0.24 on which CentOS7 is installed, running Apache 2.4, php 5.4.16 and phpMyAdmin. As far as i remember CentOS was installed headless with minimum security in a mind.

All working fine, there is a folder on host Debian which is mounted automatically in guest CentOS in /var/www/html. Any change to script in this folder is acknowledged by other side (CentOS) in sense that if I refresh browser in host which reads that script the change is displayed.

I would like to configure it the way that any file ending in .html would be parsed as php - right now any php code within such html file is kindly ignored :’(
Obviously, once the file extension is changed from .html to .php it works without any problem.

Googled it some and come across a solution which somehow did not work for me (at least for what i tried): .htaccess

The question is - instead of creating some extra configuration file can the same be achieved by configuring either php.ini or perhaps httpd.conf?

Well you know, i am stubborn muppet & could have installed xamp or something like that but why not to make the life much more difficult?

First question is why? I’ve never seen anyone actually do this anywhere. Usually this is because of some URL naming ideas and these are not relevant after setting up pretty urls in your web server. Ie rewriting yoursite.com/cars/list.html to yoursite.com/index.php?controller=cars&action=list

Hi Jim,

Completely forgot to ask for an opinion in original post whether it is worth of effort…

This is just my own setup for my personal learning, sometimes i come across such a code (forum’s beginners section, book etc…)
So I do not need it that desperately, just being curious, if I could get html done by browser directly and only php bits by php.

Looks like some studying on pretty url & apache config would be beneficial?

a perfectly normal file in a project like yours could be

list.php
[php]<?php

$cars = someMethodToGetAnArrayOfCars();

?>
<!doctype html>

The HTML5 Herald

Cars

    <?php foreach ($cars as $car): ?>
  • Make: <?= $car['make'] ?>, model: <?= $car['model'] ?>
  • <?php endforeach; ?>
[/php]

Quite common to have HTML in php files and only do bits of PHP around where you need to :slight_smile:

Thanks a lot Jim.

The key here is to name the file as .php (like your example list.php).
If this is changed to list.html then obviously php bits will not do much - that is precisely the reason why i wanted to know more about .htaccess.
Thinking that eventually by using .htaccess ( ,AddType application/x-httpd-php .html .htm) i would achieve exactly the same as if naming file directly as .php , just was not sure if there was no trick hidden/bad practice in doing it that way…

I am perfectly fine with the example, in fact that’s what I have been doing anyway - it just takes me bit longer to digest all info lol… :smiley:

Thanks a lot for your time & help.

The most common practice is to chop off the extension anyway. You don’t want html files executing php code for a number of reasons.

Ok folks, thanks for shedding some light on to this.

Will study on this topic at some point later knowing what direction to take.

Sponsor our Newsletter | Privacy Policy | Terms of Service