htaccess Rewrite question: file renaming

Discussion in 'Apache Migration/Compatibility' started by frankvh, Jul 28, 2010.

  1. frankvh

    frankvh Member

    My apologies for a noob question, but I've put a lot of time and googling into this yet I still haven't solved it. So help please!

    I have a index.html file which is auto-generated by my tools (hence I don't want to manually rename it, because then I'd have to manually rename it every single time...) which contains PHP code. So I need to push it through the PHP parser. But only that one file.

    In Apache in the .htaccess file I can do this:

    <Files frankandcolleen/index.html>
    RemoveHandler .html
    AddType application/x-httpd-php .html

    This does not work on litespeed; my webhost tells me their litespeed doesn't support the Files directive.

    I thought perhaps a rewrite might do the trick. Just to rename the file. Maybe something like this:

    RewriteEngine on
    RewriteRule frankandcolleen/index.html frankandcolleen/index.php [L]

    This doesn't work either. Probably for a whole bunch of reasons... :)

    Right now as an interim measure I'm doing this:

    RemoveHandler .html
    AddType application/x-httpd-php .html

    This works, but it forces every single html file through the PHP parser, which is both slow, and CPU intensive on the server.

    How do I push that html file, and only that file, through the PHP parser? Or rename that html file into a php file?

  2. NiteWave

    NiteWave Administrator

    how about put
    somewhere so your rewrite rule wil work:
  3. frankvh

    frankvh Member

    Thanks, I can see that idea, but where to put it? The copy command is not permitted in .htaccess (although I'll confess I tried it anyway). Create a cron job and have it execute every hour? This needs to still work when the index.html file is automatically updated by the tools, which is why I really don't want to do anything manually.

    It seems strange to me that something that can be done so simply in apache can't be done in litespeed? I've gotta be missing something here.
  4. NiteWave

    NiteWave Administrator

    questions: and how often do you update index.html?

    2.are there any other .php/.html files under frankandcolleen folder?
  5. frankvh

    frankvh Member

    1) Every 2 or 3 weeks.

    2) There are a large number of pure html files (a little over a thousand I think) but index.html is the only html file containing PHP code. There are also three .php files.
  6. mistwang

    mistwang LiteSpeed Staff

    Force index.html as MIME type "application/x-httpd-php"
    RewriteRule frankandcolleen/index.html - [T=application/x-httpd-php]
  7. frankvh

    frankvh Member

    Tried it, with and without the hypen "-" in the middle; sorry, didn't work.

    Currently the only thing that's working is the global:
    AddType application/x-httpd-php .html

    The thing that bugs me is, doesn't RewriteRule have a really crazy syntax? Well I think it does for apache - no idea about differences for litespeed.
    www .
    So I wonder if you can even just type in filenames like that. I've been attempting variations on this kind of a theme:

    RewriteRule ^frankandcolleen/index\.html$ frankandcolleen/index\.html [T=application/x-httpd-php, L]

    No luck.
  8. mistwang

    mistwang LiteSpeed Staff

    I do not think the rule really matches the request. You need to put .htaccess under directory "frankandcolleen", use rule
  9. frankvh

    frankvh Member

    Thanks for the suggestion, but is there any chance one of the litespeed staff could actually try this? Because none of these great suggestions, or any variations of them I try, are actually working.

    Thanks! I really do appreciate the help.
  10. mistwang

    mistwang LiteSpeed Staff

    Yes, I tried it in our lab, it works well.
    Maybe .htaccess was not enabled, rewrite engine is not enabled or you used wrong URL to test it.
    The rule only works with URL ending with "index.html", if you using URL like "...frankandcollen/" it wont work.
  11. frankvh

    frankvh Member


    Thank you. Point well taken. So this doesn't work too well in practice for index files, because unless you specifically type in the index.html extension (and who does that?? I just type when I want cnn for example) then it doesn't work.

    OK, thank you, that makes sense. So this is not the right path to be going down.

    I think I'm going to experiment with a cron job.
  12. frankvh

    frankvh Member

    So as we'd expect, a cron job does the trick. Perhaps not the most elegant, but it works. The cron job copies index.html to index.php. Then in .htaccess, just to be safe, I put this:

    DirectoryIndex index.php index.html

    simply to be extra-sure that the php file would be used in preference to the html file.

    I used the -u parameter for cp (ie: cp -u index.html index.php) just to prevent unnecessary overwrites of the php file.
  13. NiteWave

    NiteWave Administrator

    I tested
    as well. it works.

    another way I tried working is to create a hard link(not soft symbol link):
    with this method cron job not needed.
  14. frankvh

    frankvh Member

    Yes, sort-of. I tried your suggestion:

    It does indeed work if I do this:

    But not if I do this (which unfortunately is the normal way of doing it)

    I'm not sure why the server makes a distinction between these two cases, but it obviously does.

    Your hard link idea
    is a great idea - I'll do that. As you point out, then the cron job isn't needed anymore.
  15. mistwang

    mistwang LiteSpeed Staff

    add another rewrite rule

    RewriteRule ^$ index.html


Share This Page