文件扩展名被隐藏但在原始php内容中打开页面链接?

时间:2013-02-13 14:30:22

标签: php .htaccess

我正在尝试从我网站上的文档中删除文件扩展名.php,因此网址会像这样: http://www.mysite.com/home/
而不是像这样:
http://www.mysite.com/home.php

我尝试了各种各样的mod_rewrite代码,除了这一个,它们都没有对我有用:

#Enable rewrite engine
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.mysite.com/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.mysite.com/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

我对.htaccess一无所知,而且我是php的新手,所以我基本上无法理解这一点。目前这隐藏了.php,但它以原始格式/文本喷出页面php内容而不是像这样执行:

<?php
    $page_title = "Home";
    include('includes/header.php'); 
    include('includes/mod_home/mod_intro.php');
    include('includes/copyrightbar.php'); 
    include ('includes/mod_login/login_form.php');
    include('includes/mod_home/mod_welcome.php');
    include('includes/mod_home/mod_platinum.php');

?>

<?php include('includes/mod_home/mod_starlight.php'); ?>
<?php include('includes/mod_home/mod_diamond.php'); ?>
<?php include('includes/mod_home/mod_newest.php'); ?>
<?php include('includes/mod_home/mod_featured.php'); ?>
<?php include('includes/mod_home/mod_gold.php'); ?>


<?php include('includes/footer.php'); ?>

为什么会这样?我已经关注了所有在线教程,其他人似乎都没有这样的问题。我的服务器支持.htaccess并运行Linux 我的链接仍然是home.php例如(我需要更改这些吗?)

1 个答案:

答案 0 :(得分:0)

您错过的是RewriteBase命令 这是代码:

#Start rewriting after /
RewriteBase /
#ignore real directories
RewriteCond %{REQUEST_FILENAME} !-d
#ignore real files
RewriteCond %{REQUEST_FILENAME} !-f
#Rewrite the match to .php file
RewriteRule ^(.*)$ $1.php [QSA,L]

可能有某些原因导致PHP无法执行。所以,当然,试试这个:

AddType application/x-httpd-php .php

你可以改写为GET paremater页面,就像这样:

RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]