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



我使用的是这个代码

 # 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>

它不会在我的页面中隐藏.php扩展,当我从锚标记中删除".php"时,页面会被重定向。例如,如果url是www.example.com/mysite/store.php,并且当我从URI或锚href中删除".php"时,新的url将变为www.example.com/store/,这是不可用的。我尝试手动将url输入为example.com/mysite/store,但它也不可用。

显然.htaccess没有使用这些规则,如果我在那里写一些胡言乱语,就会得到一个500服务器的错误,所以.htacccess工作得很好,但问题是我在做什么。

忠告。谢谢你的建议!

更新

好吧,我已经想通了,我成功地实现了我提出的问题。但我的bootsrap完全坏了,我想是因为它将"bootstrap.css"链接为"bootstrat.css.php",因为我现在使用的.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中的引导链接后,我得到了不同的结果。问题是:localhost/example/index.php是我的主页,但localhost/example是url中显示的内容。localhost/example是一个目录,里面有文件。所以应该作为localhost/example/image/logo.png加载的图像被作为localhost/example/index.php/image/logo.png加载,这使得示例站点的行为很奇怪。

检查已发布的答案:

使用.htaccess 删除.php扩展

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]

最新更新