douliao7354 2014-02-23 06:23 采纳率: 0%
浏览 61
已采纳

删除URL .php扩展名会导致404错误

I am rewriting my url to get rid of the .php extension (and hopefully the get variables too as all the help I have gotten is great, but still wont work.

Here is my code:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php[\s?] [NC]
RewriteRule ^ %1 [R=301,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([^\.]+?)/?$ $1.php [NC,L] 

Now when I use this code, it takes the .php out, but then the error says:

Not Found

The requested URL /index.php was not found on this server.

Meanwhile, if I take that code out, it works fine, but has the .php extension

  • 写回答

2条回答 默认 最新

  • douzhang6646 2014-02-23 06:31
    关注

    I use this code in .htaccess to remove .php extension and redirect .php extension pages to non .php extension pages :

    <IfModule mod_rewrite.c>
     RewriteEngine On
     RewriteRule ^(wp-admin)($|/) - [L] # You don't want to mess with WordPress 
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteCond %{REQUEST_FILENAME}\.php -f
     RewriteRule .* $0.php
    
     # browser requests PHP
     RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
     RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
    
     # check to see if the request is for a PHP file:
     RewriteCond %{REQUEST_FILENAME}\.php -f
     RewriteRule ^/?(.*)$ /$1.php [L]
    </IfModule>
    

    When the above code is used in .htaccess, you can access PHP pages without the extension and when a user visits a page with .php extension, he/she will be redirected to the page without the extension.

    Example :

    http://example.com/page.php is redirected to http://example.com/page

    http://example.com/page will be the same as http://example.com/page.php

    There wouldn't be any problem when you access PHP files with extension internally :

    <?
    include("page.php");
    ?>
    

    will work without any problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 labelme打不开怎么办
  • ¥35 按照图片上的两个任务要求,用keil5写出运行代码,并在proteus上仿真成功,🙏
  • ¥15 免费的电脑视频剪辑类软件如何盈利
  • ¥30 MPI读入tif文件并将文件路径分配给各进程时遇到问题
  • ¥15 pycharm中导入模块出错
  • ¥20 Ros2 moveit2 Windows环境配置,有偿,价格可商议。
  • ¥15 有关“完美的代价”问题的代码漏洞
  • ¥15 请帮我看一下这个简易化学配平器的逻辑有什么问题吗?
  • ¥15 暴力法无法解出,可能要使用dp和数学知识
  • ¥15 wpf通过绑定控件自身的值,来实现背景颜色的切换