开发者

Default to PHP extension on web server

开发者 https://www.devze.com 2023-04-06 22:28 出处:网络
Is there a way a .htaccess file can be written so that http://www.mysite.com/about loads about.php and the same goes for any file?

Is there a way a .htaccess file can be written so that

http://www.mysite.com/about 

loads about.php

and the same goes for any file?

I tried

RewriteEngine On
RewriteRule ^([0-9A-Za-z]+)$ $1.php

but it doesn't se开发者_开发知识库em to work.


This is the most common rule (utilising mod_rewrite -- make sure it is loaded and enabled) -- it will ensure that such .php file does exist before rewriting (yes, it's a bit slower but safer):

Options +FollowSymLinks
RewriteEngine On

# add .php file extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [L]

Alternatively just use this:

Options +MultiViews

This will turn on "content negotiation". But it has some cons.

For example: let's assume you have hello.html & hello.php in your website root folder. If you request example.com/hello, with that option enabled Apache will look for alternative names (same name but different extensions) and will serve either hello.html or hello.php (I cannot tell which one will be preferred).

But if you only have 1 file with such unique name (e.g. hello.php ONLY) then no problems here at all.

0

精彩评论

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