我有.htaccess文件,profile.php,index.php和test.php。
假设我们有用户john
如果我去网站http://example.org/john,那么我可以看到john个人资料。但是,如果我要转到index.php或test.php,那么页面就会出现错误404,但不应该如此。似乎htaccess文件中的代码正在破坏每一页。
如何修复profile.php和.htaccess文件以获得:
http://example.org/john不存在,则在同一页上显示404错误,如果存在,则显示此配置文件test.php)。代码:
.htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
DirectorySlash Off
RewriteRule ^([A-Za-z0-9\._\-]+)[^.]$ profile.php?user=$1 [NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]profile.php
<?php
if ($_GET['user'] != '') {
echo 'Showing user profile';
} else {
echo '404 error';
}发布于 2019-03-03 23:48:06
您不能从php执行返回到apache。
您应该交换RewriteRules,因此首先执行文件到php,然后执行配置文件。
在不需要额外重写的规则之后添加L。
查看http://php.net/manual/en/function.header.php以发送正确的404响应。设置404作为第三个参数。
https://stackoverflow.com/questions/54974744
复制相似问题