无法从我的网站隐藏.php扩展程序,我该怎么做?

时间:2016-05-05 10:10:19

标签: php .htaccess

我正在使用此代码

 # Apache Rewrite Rules
 <IfModule mod_rewrite.c>
  Options +FollowSymLinks
  RewriteEngine On
  RewriteBase /

# Add trailing slash to url
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
  RewriteRule ^(.*)$ $1/ [R=301,L]

# Remove .php-extension from url
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}\.php -f
  RewriteRule ^([^\.]+)/$ $1.php 

# End of Apache Rewrite Rules
 </IfModule>

当我删除&#39; .php&#39;时,它不会隐藏我网页上的.php扩展名。从我的锚标签,页面被重定向。 例如。如果网址为www.example.com/mysite/store.php,当我删除&#39; .php&#39;从URI或锚点href开始,新网址变为www.example.com/store/,当然不可用。我尝试手动输入网址example.com/mysite/store,但它也不可用。

显然.htaccess没有使用这些规则,如果我在那里写乱码,得到500服务器错误,那么.htaccess工作正常,但我做错了。

建议。 谢谢你的建议!

更新

好的,我已经弄明白了,我成功地实现了我的问题。但是我的靴子完全被打破了,我猜是因为它链接了&#39; bootstrap.css&#39; as&#39; bootstrap.css.php&#39;因为我现在使用的.htaccess文件是:

# Turn on the rewrite engine
RewriteEngine  on
# If the request doesn't end in .php (Case insensitive) continue processing rules
RewriteCond %{REQUEST_URI} !\.php$ [NC]
# If the request doesn't end in a slash continue processing the rules
RewriteCond %{REQUEST_URI} [^/]$
# Rewrite the request with a .php extension. L means this is the 'Last' rule
RewriteRule ^(.*)$ $1.php [L]

DefaultType application/x-httpd-php
DirectoryIndex index.php index.html

更新2

对不起,这不是我在上次更新中提到的问题,因为我在index.php中删除了bootstrap链接后,得到了不同的结果。问题是:localhost/example/index.php是我的主页,但localhost / example是url中显示的内容。 localhost/example是包含文件的目录。因此,应加载为localhost/example/image/logo.png的图片作为localhost/example/index.php/image/logo.png加载,使示例网站表现得很奇怪。

1 个答案:

答案 0 :(得分:1)

检查已发布的答案:

Remove .php extension with .htaccess

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]