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

Apache Mod Rewrite重定向问题求助:部分请求无法跳转至指定页面

问题解决步骤

1. 修复非根路径请求的重写规则

你当前的重写规则存在语法错误,最后一行RewriteRule ^(.*)$ \ [L,QSA]的目标路径写为\,导致非根路径(如example.com/something)无法正确跳转至index.html。修正后的完整配置如下:

<Directory "/var/www/html">
RewriteEngine On
RewriteBase /
# www域名跳转至非www域名(自动适配当前请求协议)
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ %{REQUEST_SCHEME}://%1/$1 [R=301,L]

# 所有非文件、非目录的请求重定向到index.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.html [L,QSA]
</Directory>

2. 解决HTTPS请求不生效的问题

https://example.com无法重定向,是因为当前配置仅作用于HTTP(80端口)站点,HTTPS(443端口)的站点配置通常在/etc/apache2/sites-enabled/default-ssl.conf文件中,需要在该文件的对应<Directory>块中添加上述完全相同的重写规则。

若尚未启用SSL模块和默认SSL站点,先执行以下命令:

sudo a2enmod ssl
sudo a2ensite default-ssl.conf

修改完成后,重启Apache服务使配置生效:

sudo systemctl restart apache2

额外注意事项

  • %{REQUEST_SCHEME}会自动识别请求协议(http/https),避免硬写协议导致跳转错误
  • 测试阶段可将[R=301]替换为[R=302]临时重定向,确认规则无误后再改用永久重定向
  • 确保mod_rewrite模块已启用,未启用则执行sudo a2enmod rewrite后重启服务

内容的提问来源于stack exchange,提问作者Gabriel Philips

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 22:16:09