开发者

Must a webpage end in ".php" in order to run php code?

Does a webpage need to end in ".php" in order to run php code? Is there a way a f开发者_C百科ile ending in ".html" can run php code?


If you're using the Apache web server you can add this line to its configuration:

AddType application/x-httpd-php .html

it tells the the server that files with a .html extension have to be considered as PHP files. I don't recommend this though, as it forces the PHP engine on every file (even static html pages).

Alternatively you can rewrite (some) .html URLs to their PHP version via mod_rewrite.


No a webpage doesn't need to end in .php to be parsed as PHP.

Yes you can use any extension you want depending on your web server, for example the below code placed in the Apache configuration would parse files with a .htm, .html or .php extension as PHP if running on an Apache server.

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

And you may also try this with regular expressions to better match specific criteria:

<FilesMatch "\.(htm|html|php)$">
    SetHandler application/x-httpd-php
</FilesMatch>


You can have PHP parse any file type, as long as you tell it to.

In .htaccess:

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

This tells PHP to parse .php and .html files.


You could also eliminate the extension altogether with this.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php

So now http://www.test.com/mypage.php will be http://www.test.com/mypage


I suggest you look into URL Rewriting instead of messing up the .html configuration in Apache. This way you can have php pages end in anything you want or even not have any extension at all. Extensionless SEO friendly URLs are always better than any existing filenames with extension.

Just my 2 coins :)


Yes, there is a way: associate the .html extension with PHP in the Apache/other webserver config.

However, it's not recommended to make PHP pages look like HTML, it's better to crop the extension as is.


Yes, you can do that with .htaccess:

RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\.html$ $1.php [L]


The change you make to your .htaccess file can vary by host.

On mediatemple, for example, you add this ::

SetHandler php-script

See mediatemple documentation on this topic here

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜