如何通过.htaccess将带.php后缀旧URL重定向到无后缀SEO友好URL
实现旧URL到新URL的自动重定向
问题背景
我的网站旧URL结构如下:
https://example.com/login.php or, https://example.com/login.php?redirect=https://example.com or, https://example.com/register.php
重构URL后,新URL结构为:
https://example.com/login or, https://example.com/login?redirect=https://example.com or, https://example.com/register
希望用户访问旧URL时自动重定向到对应的新URL,现有.htaccess文件内容如下:
AddDefaultCharset UTF-8 ErrorDocument 404 /error-404/ ## Options +FollowSymlinks -MultiViews RewriteEngine on RewriteBase / ## Allow a few SEO Files direct access. RewriteRule ^robots.txt?$ robots.txt [L] RewriteRule ^ads.txt?$ ads.txt [L] RewriteRule ^sellers.json?$ sellers.json [L] ## Avoid rewriting rules for the admin section RewriteRule ^(admin|resources)($|/) - [L] ## Set Ajax Request File RewriteRule ^kahuk-ajax.php?$ kahuk-ajax.php? [L,QSA] ## Set controller with id RewriteRule ^([^/]+)/([0-9]+)/?$ index.php?con=$1&id=$2 [L,QSA] ## Set controller with slug RewriteRule ^([^/]+)/([^/]+)/?$ index.php?con=$1&slug=$2 [L,QSA] ## For paging RewriteRule ^([^/]+)/page/([0-9]+)/?$ index.php?con=$1&page=$2 [L,QSA] RewriteRule ^([^/]+)/([^/]+)/page/([0-9]+)/?$ index.php?con=$1&slug=$2&page=$3 [L,QSA] ## Set controller for only one parameter RewriteRule ^page/([^/]+)/?$ index.php?con=page&slug=$1 [L,QSA] RewriteRule ^([^/]+)/?$ index.php?con=$1 [L,QSA] ## Set home page RewriteRule ^/?$ index.php?con=home [L]
实现方法
要实现旧URL到新URL的重定向,需要在.htaccess的RewriteEngine on和RewriteBase /之后添加专门的重写规则,必须放在其他重写规则之前,确保旧URL优先被匹配处理。
添加的规则如下:
## 重定向旧的.php登录/注册URL到新URL RewriteRule ^login\.php$ /login [R=301,L,QSA] RewriteRule ^register\.php$ /register [R=301,L,QSA]
规则说明
^login\.php$:精确匹配以login.php结尾的旧URL/login:对应的新URL路径R=301:设置永久重定向,对搜索引擎SEO友好L:标记为最后执行的规则,匹配后不再处理后续规则QSA:自动保留原URL的查询参数(比如redirect参数会完整传递到新URL中)
修改后的完整.htaccess文件
AddDefaultCharset UTF-8 ErrorDocument 404 /error-404/ ## Options +FollowSymlinks -MultiViews RewriteEngine on RewriteBase / ## 重定向旧的.php登录/注册URL到新URL RewriteRule ^login\.php$ /login [R=301,L,QSA] RewriteRule ^register\.php$ /register [R=301,L,QSA] ## Allow a few SEO Files direct access. RewriteRule ^robots.txt?$ robots.txt [L] RewriteRule ^ads.txt?$ ads.txt [L] RewriteRule ^sellers.json?$ sellers.json [L] ## Avoid rewriting rules for the admin section RewriteRule ^(admin|resources)($|/) - [L] ## Set Ajax Request File RewriteRule ^kahuk-ajax.php?$ kahuk-ajax.php? [L,QSA] ## Set controller with id RewriteRule ^([^/]+)/([0-9]+)/?$ index.php?con=$1&id=$2 [L,QSA] ## Set controller with slug RewriteRule ^([^/]+)/([^/]+)/?$ index.php?con=$1&slug=$2 [L,QSA] ## For paging RewriteRule ^([^/]+)/page/([0-9]+)/?$ index.php?con=$1&page=$2 [L,QSA] RewriteRule ^([^/]+)/([^/]+)/page/([0-9]+)/?$ index.php?con=$1&slug=$2&page=$3 [L,QSA] ## Set controller for only one parameter RewriteRule ^page/([^/]+)/?$ index.php?con=page&slug=$1 [L,QSA] RewriteRule ^([^/]+)/?$ index.php?con=$1 [L,QSA] ## Set home page RewriteRule ^/?$ index.php?con=home [L]
内容的提问来源于stack exchange,提问作者Shah Alom
相关产品推荐
相关产品推荐

