douzhiba6873 2014-09-03 05:06
浏览 128

自动URL重写不起作用

I want to hide .php extension so I wrote following code in .htaccess file which I found at this link How to remove file extension from website address?

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

This work fine when I type localhost/testsite/index it displays correctly localhost/testsite/index.php and display localhost/testsite/index in address bar as I want, but when I forcefully type localhost/testsite/index.php it does not converted to localhost/testsite/index. I want to remove extension even when user type .php after page name.

  • 写回答

1条回答 默认 最新

  • dongzhisang5342 2014-09-03 06:08
    关注

    Have your .htaccess like this:

    DirectoryIndex index.php
    RewriteEngine On
    RewriteBase /
    
    ## hide .php extension
    # To externally redirect /dir/file.php to /dir/file
    RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]
    RewriteRule ^ /%1%2 [R=302,L,NE]
    
    # To internally forward /dir/file to /dir/file.php
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
    RewriteRule ^(.+?)/?$ $1.php [L]
    
    评论

报告相同问题?

悬赏问题

  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了