htaccess 正则表达式 301 重定向

问题描述 投票:0回答:1

我很难让永久重定向工作。我希望这发生,使用正则表达式。

旧网址: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 是实际名称,可以硬编码。我究竟做错了什么?谢谢!

regex .htaccess redirect mod-rewrite http-status-code-301
1个回答
0
投票

尝试以下操作:

  • 如果您的

    .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

© www.soinside.com 2019 - 2024. All rights reserved.