开发者

PHP code in HTML without a file extension

开发者 https://www.devze.com 2023-03-21 23:28 出处:网络
I currently have a Movable Type blog where the pages do not have any file extensions...so example.com/entries/this-is-my-entry

I currently have a Movable Type blog where the pages do not have any file extensions...so example.com/entries/this-is-my-entry

I added DefaultType text/html, so Apache knows if there isn't any extension on the end of the file to display as HTML. That works great.

Now the problem is I have PHP code in the HTML. The page won't render the PHP code if there is no extension. However, when I publish the entries with a .html extension....the PHP code works perfectly.

I have AddHandler php5-script .html to tell Apache to display PHP in the HTML files. I'm guessing, if there isn't any file extension, it doesn't know to render the PHP code, because it is expecting files with a .html extension...is there a way 开发者_StackOverflowto say any file that doesn't have an extension can use PHP?


I'm doing the same implementation for my personal MT blog, with the entries having no file extension. (The theory being that I could switch to other techniques in the future without being hampered by extensions like .html, .php, etc.)

You can accomplish this by setting your default type to be PHP:

DefaultType application/x-httpd-php

All PHP files are interpreted at first as HTML, so this works even for files with no PHP in them.


Maybe you can try using <FileMatch> directive? Something like

<FilesMatch "^[^\.]$">
    ForceType application/x-httpd-php
</FilesMatch>


There is nothing directly corresponding to DefaultType for AddHandler, but you can hack around it in a fairly ugly way:

<FilesMatch "^(?!.*\..+)">
    SetHandler php5-script
</FilesMatch>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号