尝试使用.htaccess文件删除.php扩展程序时,我会遇到403禁止错误



我想使用位于同一文件夹中的.htaccess文件从URL中删除.php扩展名(我想删除所有文件的扩展程序,而不是只是其中之一(。我使用以下代码:

Options +FollowSymLinks # I found this on the Internet trying to solve this problem, but nothing changed
RewriteEngine On
RewriteBase /app_tests/ # This is the folder where I want to delete the .php extension, it's located in the htdocs folder
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*.php$ %{REQUEST_FILENAME}.php [QSA,L] #I have tried without QSA as well (I don't really know what it does)

因此,从理论上讲,当我访问myweb.com/index时,我应该查看index.php页面,但输入该地址时会遇到403错误(禁止(。当然,没有目录称为索引。我知道该代码有效(或至少做某事(,因为当我写错误时,它会显示内部服务器错误,因为如果我擦除或评论它(#((#(我遇到的错误是404(找不到(。

我正在使用Windows的XAMPP的最后版本之一。我已经在httpd.conf文件中删除了以下行,已加载模块:

LoadModule rewrite_module modules/mod_rewrite.so

有人知道我做错了什么或如何解决这个问题吗?谢谢!

它可能失败了,因为您在指令之后发表了评论。在.htaccess的评论必须在自己的行上,它们不能遵循规则/条件。

我将通过分享堆栈中的片段来帮助您完成手头的任务。将此.htaccess文件放入您想要.php的文件访问工作的文件夹中。

<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
RewriteBase /app_tests/
# Set an S for HTTPS protocol
# helps for rewrites with protocol specific URL
RewriteCond %{SERVER_PORT}s ^(443(s)|[0-9]+s)$
RewriteRule ^(.+)$ - [ENV=SFORSSL:%2]
# remove trailing slashes if it's not a directory
# you will need this, because if a trailing slash is added wrongly
# the condition for the next group of rules will fail
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ http%{ENV:SFORSSL}://%{HTTP_HOST}/$1 [R=301,QSA,L]
# internally rewrite to .php if a file exists
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.+) $1.php [QSA,L]
</IfModule>

QSA,代表查询字符串附加,它基本上将get请求包含的所有请求参数附加(在url中之后的所有内容(。

最新更新