【问题标题】:Basic 301 Redirect code not working基本 301 重定向代码不起作用
【发布时间】:2015-05-21 19:51:44
【问题描述】:

我的页面由于某种原因没有重定向。我正在尝试在我的 htaccess 文件中使用以下代码。

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

RewriteEngine On
Options +FollowSymLinks

RedirectMatch 301 ^/([^-]+)-([^-]+)-([^-]+)-([^-]{2})\.html$ /$1-$2-in-$3-$4.html
RedirectMatch 301 ^/([^-]+)-([^-]+)-([^-]+)\.html$ /$1-$2-in-$3.html

# 301 --- http://www.domain.com/music-classes-san-diego-ca.html => http://www.domain.com/music-classes-in-san-diego-ca.html
RewriteRule ^music-classes-san-diego-ca\.html$ /music-classes-in-san-diego-ca.html? [L,R=301]

301 重定向是我想要开始工作的。它不是为我重定向。我也试过这个:

redirect 301 music-classes-san-diego-ca.html http://www.domain.com/music-classes-in-san-diego-ca.html

【问题讨论】:

    标签: .htaccess redirect


    【解决方案1】:

    任何时候使用 wordpress,这些规则总是需要放在最后,因为它将所有内容都路由到 index.php。在 wordpress 规则之前尝试你的规则。仅供参考,我个人会对所有 301 使用 Rewrite,而不是将它们与 RedirectMatch 混合使用。

    Options +FollowSymLinks
    
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^music-classes-san-diego-ca\.html$ /music-classes-in-san-diego-ca.html? [L,R=301]
    
    RedirectMatch 301 ^/([^-]+)-([^-]+)-([^-]+)-([^-]{2})\.html$ /$1-$2-in-$3-$4.html
    RedirectMatch 301 ^/([^-]+)-([^-]+)-([^-]+)\.html$ /$1-$2-in-$3.html
    
    # BEGIN WordPress
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    # END WordPress
    

    【讨论】:

    • @user1858780 没问题,很高兴它成功了。要标记答案。
    • 完成! :) 我还有一个与 htaccess 代码有关的问题。 RedirectMatch 301 ^/([^-]+)-([^-]+)-([^-]+)-([^-]{2})\.html$ /$1-$2-in-$3-$4.html RedirectMatch 301 ^/([^-]+)-([^-]+)-([^-]+)\.html$ /$1-$2-in-$3.html 上面的代码将music-classes-dallas-tx 重定向到music-classes-in-dallas-tx,它还将music-classes-texas 移动到music-classes-texas。它涵盖城市和州。我不知道如何让它与有两个词的城市一起工作,例如,圣安东尼奥。重定向匹配代码是什么?这有可能吗?
    • 我会将 3 捕获组用作任何字符,这样它就可以获得两个单词。我会做这样的事情 RedirectMatch 301 ^/([^-]+)-([^-]+)-(.+)-([^-]{2})\.html$ /$1-$2-in-$3-$4.html 这应该匹配 music-classes-in-san-antonio-tx.html
    • 谢谢 - 由于某种原因,它正在执行重定向循环,并且 url 看起来像这个 domain.com/music-classes-in-in-in-in-in-in-in-in-in- in-in-in-in-in-in-in-in-in-san-antonio-tx.html
    • @user1858780 我会更多地研究您想要匹配的内容,但您应该打开关于在新问题中留出空间的特定问题。你会得到答案的。
    猜你喜欢
    • 2018-02-24
    • 1970-01-01
    • 1970-01-01
    • 2017-07-20
    • 2013-06-05
    • 2013-11-27
    • 2014-04-30
    相关资源
    最近更新 更多