You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用.htaccess重定向除指定页面外的所有页面时出现500错误求助

问题分析与解决方案

导致500错误的直接原因

你的重定向规则里,RewriteCond %{REQUEST_URI}!^/test/ 存在语法错误:%{REQUEST_URI} 和 ! 之间必须有空格,Apache的RewriteCond语法要求条件变量和运算符之间用空格分隔,缺少空格会触发服务器内部错误(500)。

重定向规则的位置问题

另外,重定向规则必须放在原有框架路由规则的前面。原配置里的框架路由规则(转发到index.php的部分)会先处理请求,如果重定向规则放在后面,请求已经被转发到index.php,重定向就不会生效了。

修正后的完整.htaccess配置

# Enable rewrite engine and route requests to framework
RewriteEngine On

# Some servers require you to specify the `RewriteBase` directive
# In such cases, it should be the path (relative to the document root)
# containing this .htaccess file
#
# RewriteBase /

# 重定向规则:除/test/路径外,所有请求跳转到新域名
# 增加排除新域名的条件,避免循环重定向(如果新旧域名指向同一服务器)
RewriteCond %{HTTP_HOST} !^www\.newdomain\.com$
RewriteCond %{REQUEST_URI} !^/test/
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

# 原有框架路由规则,保持不变
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L,QSA]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]

# Default expires header if none specified (stay in browser cache for 10 days)
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/css A864000
    ExpiresByType application/javascript A864000
    ExpiresByType image/gif A864000
    ExpiresByType image/png A864000
    ExpiresByType image/jpeg A864000
    ExpiresByType image/x-icon A864000
</IfModule>

为什么只保留新配置会出现404?

原有的框架路由规则负责将非真实文件/目录/符号链接的请求转发到index.php(比如/test/这类动态路径),删掉原有配置后,服务器找不到/test/对应的物理资源,自然返回404错误。因此必须保留框架的路由规则,才能让/test/路径正常工作。

内容的提问来源于stack exchange,提问作者Vanbel Manik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 13:06:25