2016-08-30 104 views
0

我在StackOverflow上找到了很多解决方案来处理这个常见问题,但是似乎没有一个适用于我的设置。当我链接到一个PHP页面我仍然看到PHP扩展,并希望有重写规则以使用.htaccess删除.php扩展名

index.php updated to 'website' 

about-us.php to be website/about 

我使用WAMP的文件夹“网站”内/ localhost的站点工作。我的.htacces文件位于“网站”文件夹的根目录内。

重写条件/规则我试图用来从index.php和其他php页面中删除.php扩展名是这样的。

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php 

我的链接有URL的这样

<a href="index.php"> 
<a href="about-us.php"> 

下面是完整的.htaccess文件

RewriteEngine On 
RewriteBase/
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] 
RewriteRule ^(.*)$ http://%1/$1 [R=301,L] 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php 

## EXPIRES CACHING ## 
<IfModule mod_expires.c> 
ExpiresActive On 
ExpiresByType image/jpg "access 1 year" 
ExpiresByType image/jpeg "access 1 year" 
ExpiresByType image/gif "access 1 year" 
ExpiresByType image/png "access 1 year" 
ExpiresByType text/css "access 1 month" 
ExpiresByType application/pdf "access 1 month" 
ExpiresByType application/x-javascript "access 1 month" 
ExpiresByType application/x-shockwave-flash "access 1 month" 
ExpiresByType image/x-icon "access 1 year" 
ExpiresDefault "access 2 days" 
</IfModule> 
## EXPIRES CACHING ## 

<IfModule mod_deflate.c> 
# compress text, html, javascript, css, xml: 
AddOutputFilterByType DEFLATE text/plain 
AddOutputFilterByType DEFLATE text/html 
AddOutputFilterByType DEFLATE text/xml 
AddOutputFilterByType DEFLATE text/css 
AddOutputFilterByType DEFLATE application/xml 
AddOutputFilterByType DEFLATE application/xhtml+xml 
AddOutputFilterByType DEFLATE application/rss+xml 
AddOutputFilterByType DEFLATE application/javascript 
AddOutputFilterByType DEFLATE application/x-javascript 
</IfModule> 

# Or, compress certain file types by extension: 
<files *.html> 
SetOutputFilter DEFLATE 
</files> 

回答

1

它实际上不是 “删除” .php从URL,它可以作为重定向。 换句话说,如果您不希望其他人在您的网址中看到.php,那么您必须修复所有链接。

<a href='www.example.com'>

<a href='www.example.com/about-us'>

<a href='www.example.com/blog'>

你必须解决您的脚本中的所有链接。