如果你遇到Apache .htaccess重定向问题,以下是一些可能的解决方案和代码示例:
httpd.conf
或apache.conf
文件中找到以下行,并确保没有注释:LoadModule rewrite_module modules/mod_rewrite.so
AllowOverride All
:
AllowOverride All
RewriteEngine On
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
这将将所有请求重定向到http://example.com/
,保留原始请求的路径。
RewriteEngine On
RewriteRule ^old-page\.html$ /new-page.html [R=301,L]
这将将old-page.html
重定向到new-page.html
。
RewriteEngine On
RewriteRule ^(.+)\.html$ http://example.com/$1.php [R=301,L]
这将将以.html
结尾的所有请求重定向到以.php
结尾的URL。
请注意,上述代码示例中的R=301
表示使用永久重定向,将告诉搜索引擎和浏览器更新其索引。如果你只想使用临时重定向,可以使用R=302
。
确保在进行任何更改之前备份你的.htaccess文件,并重新启动Apache服务器以使更改生效。