首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法对我的站点隐藏.php扩展,我该如何做?

无法对我的站点隐藏.php扩展,我该如何做?
EN

Stack Overflow用户
提问于 2016-05-05 10:10:19
回答 1查看 675关注 0票数 0

我在用这个代码

代码语言:javascript
复制
 # 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/,而这个www.example.com/store/是不可用的。我试图手动输入url作为example.com/mysite/store,但它也是不可用的。

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

建议。谢谢你的建议!

更新

好吧,我已经想好了,我成功地完成了我的问题。但是我的引导文件完全失效了,我猜是因为它将'bootstrap.css‘链接为'bootstrap.css.php’,因为我现在使用的.htaccess文件是:

代码语言:javascript
复制
# 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是我的主页,但是本地主机/示例是在url中显示的。localhost/example是一个包含文件的目录。因此,应该作为localhost/example/image/logo.png加载的图像作为localhost/example/index.php/image/logo.png加载,这使得示例站点的行为很奇怪。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-05 10:36:16

核对已张贴的答案:

Remove .php extension with .htaccess

代码语言:javascript
复制
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]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37047848

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档