douluoqiu4538 2015-02-16 04:19 采纳率: 100%
浏览 83
已采纳

修改我的.htaccess文件以隐藏URL中的.php扩展名后,无法找到某些目录项

Recently, I got some help modifying my .htaccess file to hide the .php extension in the url. After I got it working, a few bugs occurred with my site. For whatever reason, any php page in a sub directory returns a 404 error. For example:

mywebsite.com/test/test.php (this 404's)

but

mywebsite.com/test/test.html (this works just fine)

Since this only started occurring after my .htaccess file was modified, I am assuming the problem lies there. The file contains as follows:

RewriteEngine On

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.mywebsite.com/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /.+?\.php [NC]
RewriteRule ^(.+?)\.php$ /$1 [R=301,L,NE]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

ErrorDocument 404 /404.php

DirectoryIndex index.php

If there is any more information I can provide to help solve this, please let me know!

Thank you for all the help.

  • 写回答

2条回答 默认 最新

  • douya2006 2015-02-16 08:06
    关注

    Try these rules in your root .htaccess:

    ErrorDocument 404 /404.php    
    DirectoryIndex index.php
    RewriteEngine On
    RewriteBase /
    
    # Unless directory, remove trailing slash
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/$ /$1 [R=301,L,NE]
    
    # Redirect external .php requests to extensionless url
    RewriteCond %{REQUEST_METHOD} !POST
    RewriteCond %{THE_REQUEST} /.+?\.php [NC]
    RewriteRule ^(.+?)\.php$ /$1 [R=301,L,NE]
    
    # Resolve .php file for extensionless php urls
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
    RewriteRule ^(.+?)/?$ $1.php [L]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 无源定位系统的时差估计误差标准差
  • ¥15 请问这个代码哪里有问题啊
  • ¥20 python--version在命令端输入结果Python is not defined怎么办?还有pip不是exe格式是不是没安装成功?
  • ¥15 通过GaussianView进行结构微调消除虚频
  • ¥15 调用transformers库
  • ¥15 由于导出的数据名字中带有/,导致Matlab打不开,怎么办?
  • ¥15 新硬盘安装的程序总是崩溃,提示遇到错误
  • ¥15 openpcdet自制数据集评估bev精度和3d精度相同
  • ¥15 excel 上下按钮 显示行
  • ¥20 云卓h12pro 数传问题