.htaccess配置异常:无法将example.com重定向至example.com/home
解决example.com重定向至example.com/home的.htaccess配置问题
要实现将example.com(含www子域)重定向至example.com/home,在原有cPanel生成的.htaccess基础上添加重定向规则后出现500 Internal Server Error,原配置存在语法错误和逻辑问题。
原配置的问题点
- RewriteRule目标URL使用了中文双引号
"http://example.com/home",Apache无法识别,引发语法错误 - 未排除
/home路径本身,会导致重定向循环(访问/home时仍会被强制重定向) - HTTP_HOST条件错误写成了
example.ir,与目标域名example.com不匹配
正确的.htaccess配置
保留原有cPanel生成的PHP handler代码,添加修正后的重定向规则:
# php -- BEGIN cPanel-generated handler, do not edit # Set the “ea-php81” package as the default “PHP” programming language. <IfModule mime_module> AddHandler application/x-httpd-ea-php81 .php .php8 .phtml </IfModule> # php -- END cPanel-generated handler, do not edit. # Redirect to example.com/home <IfModule mod_rewrite.c> RewriteEngine on RewriteBase / # 排除index.html和/home路径,避免循环 RewriteRule ^index\.html$ - [L] RewriteRule ^home/?$ - [L] # 匹配example.com或www.example.com(忽略大小写) RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC] # 仅对网站根目录进行重定向,不影响其他子页面 RewriteRule ^$ http://example.com/home [R=301,L] </IfModule>
配置说明
[NC]标记忽略域名大小写,兼容不同大小写的访问请求RewriteRule ^home/?$ - [L]用于排除/home路径,彻底避免循环重定向- 合并两个HTTP_HOST条件为一个正则表达式
^(www\.)?example\.com$,简化规则 - 去掉目标URL的引号,使用Apache标准语法格式
- 匹配规则改为
^$,仅对网站根目录(example.com/)触发重定向,不会影响其他子页面(如example.com/about)的正常访问
内容的提问来源于stack exchange,提问作者MB_18
相关产品推荐
相关产品推荐

