修改我的.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.
最近,我得到了一些帮助修改我的.htaccess文件以隐藏网址中的.php扩展名。 在我开始工作之后,我的网站出现了一些错误。 无论出于何种原因,子目录中的任何php页面都会返回404错误。 例如: p>
mywebsite.com/test/test.php(the 404's) p> blockquote>
但是 p>
mywebsite.com/test/test.html(这很好用) p> blockquote>
因为这个 仅在我的.htaccess文件被修改后才开始发生,我假设问题就在那里。 该文件包含如下: p>
RewriteEngine On #除非目录,删除尾部斜杠 RewriteCond%{REQUEST_FILENAME}!-d RewriteRule ^([^ /] +)/ $ http://www.mywebsite.com/$1 [R = 301,L] #将外部.php请求重定向到无扩展名url RewriteCond%{REQUEST_METHOD}!POST RewriteCond%{THE_REQUEST} / 。+?\ .php [NC] RewriteRule ^(。+?)\ .php $ / $ 1 [R = 301,L,NE] #解析.php文件for extendedless php urls RewriteRule ^([ ^ /。] +)$ $ 1.php [L] NodeDocument 404 /404.php DirectoryIndex index.php code> pre>
如果有的话 我可以提供更多信息来帮助解决这个问题,请让我知道! p>
感谢您的帮助。 p> div>
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]
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
Add the following line to your .htaccess file.
Options -Indexes