我很难让永久重定向工作。我希望这发生,使用正则表达式。
旧网址:https://example.com/olddir/other_name_here/123456/garbage.jpg
新网址:https://example.com/newdir/other-name-here-123456/
请注意从下划线到破折号的变化,以及我在数字字符串后丢弃额外位的事实。我已经试过了,但它不起作用(页面不存在,但仍然收到 404):
RewriteRule ^/olddir/other_name_here/([0-9]{6})/.+ /newdir/other-name-here-$1/ [R=301,L]
我在“other_name_here”目录位置有几百个名字,所以如果我可以动态地将下划线更改为连字符,那会很好但不是必需的。 olddir 和 newdir 是实际名称,可以硬编码。我究竟做错了什么?谢谢!
尝试以下操作:
如果您的
.htaccess
中没有出现,请在您的RewriteRule
之前添加以下两行:
Options +FollowSymLinks
RewriteEngine On
通过添加
$
以下列方式更改您的正则表达式:
RewriteRule ^/olddir/other_name_here/([0-9]{6})/.+$ /newdir/other-name-here-$1/ [R=301,L]
尝试直接访问您的新 URL 页面并确认它们可以访问。
最后,我会推荐这个网站了解详细信息
.htaccess
详细配置:https://mediatemple.net/community/products/dv/204643270/using-htaccess-rewrite-rules