[英]After modifying my .htaccess file to hide the .php extension from the URL, some directory items are unable to be found
Recently, I got some help modifying my .htaccess file to hide the .php extension in the url. 最近,我得到了一些帮助修改我的.htaccess文件来隐藏url中的.php扩展名。 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.
无论出于何种原因,子目录中的任何php页面都会返回404错误。 For example:
例如:
mywebsite.com/test/test.php (this 404's)
mywebsite.com/test/test.php(404)
but 但
mywebsite.com/test/test.html (this works just fine)
mywebsite.com/test/test.html(这很好用)
Since this only started occurring after my .htaccess file was modified, I am assuming the problem lies there. 由于这只是在我的.htaccess文件被修改后才开始发生,我假设问题就在那里。 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. 谢谢你的帮助。
Try these rules in your root .htaccess: 在你的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]
If you are intended to hide your PHP project structure to be accessed by public as directory tree ... you should consider adding a .htaccess file in the root of your project with just 1 line 如果您打算隐藏您的PHP项目结构以供公共访问作为目录树...您应该考虑在项目的根目录中添加.htaccess文件,只需1行
Add the following line to your .htaccess file. 将以下行添加到.htaccess文件中。
Options -Indexes
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.