【问题标题】:Preventing access to seo-unfriendly URLs and redirect to seo-friendly (canonical) URLs阻止访问对 seo 不友好的 URL 并重定向到对 seo 友好(规范)的 URL
【发布时间】:2016-01-03 08:06:54
【问题描述】:

我看到很多类似的问题,但到目前为止没有一个对我有帮助:

我在 /song/song.php?url=lang/params 下有我的脚本逻辑(对 seo 不友好的 URL)。我使用这种重写来获得一个友好的 URL,并从对 seo 不友好的 URL 加载内容,使用参数来区分语言:

 RewriteRule ^songs-and-chords/(.*?)(/*)?$ /song/song.php?url=en/$1 [NC,L]
 RewriteRule ^canzoni-e-accordi/(.*?)(/*)?$ /song/song.php?url=it/$1 [NC,L]

现在我想阻止访问 /song/song.php,强制重定向到对 seo 友好的,所以像这样,使用 %{THE_REQUEST} 进行外部重定向:

 RewriteCond %{THE_REQUEST} .*?song/song\.php\?url=it/(\S*) [NC]
 RewriteRule /canzoni-e-accordi/$1 [R,L]
 RewriteCond %{THE_REQUEST} .*?song/song\.php\?url=en/(\S*) [NC]
 RewriteRule /songs-and-chords/$1 [R,L]
 RewriteCond %{THE_REQUEST} ^.*?song/song\.php.*$ [NC]
 RewriteRule /songs-and-chords/ [R,L]

但是没有执行此规则...我做错了什么?我怎样才能以一种好的方式调试它?

(我已经在 song.php 脚本中使用了 rel="canonical" 但我仍然希望该脚本根本无法访问)。

【问题讨论】:

    标签: .htaccess mod-rewrite url-redirection friendly-url


    【解决方案1】:

    您做错了,您的 RewriteRule 语句的模式丢失了。此外,重写后的 URL 将使用 %1 引用而不是 $1

    RewriteCond %{THE_REQUEST} ^GET\ /song/song\.php\?url=it/(\S+) [NC]
    RewriteRule ^ /canzoni-e-accordi/%1? [R=301,L]
    RewriteCond %{THE_REQUEST} ^GET\ /song/song\.php\?url=en/(\S+) [NC]
    RewriteRule ^ /songs-and-chords/%1? [R=301,L]
    RewriteCond %{THE_REQUEST} ^GET\ /song/song\.php\s [NC]
    RewriteRule ^ /songs-and-chords/ [R=301,L]
    

    【讨论】:

    • 谢谢,没错。唯一的问题是:这个 URL /song/song.php?url=it/pink-floyd/the-wall 重定向到 /canzoni-e-accordi/pink-floyd/the-wall?url=it/pink-floyd/ the-wall 我怎样才能摆脱附加的 url 参数? QSA 标志未设置,那么它来自哪里? (GET后的“\”是什么)?
    • @Fabbio 将 %1 更改为 %1? 将修复重定向 url 中添加的查询字符串。至于GET\ 字符串;发送到服务器的请求格式为:GET /song/song.php?url=it/pink-floyd/the-wall HTTP/1.1。我在GET 之后逃离空间。您也可以使用\s 代替\
    • 像魅力一样工作!但是我现在有点迷茫...... %1 是什么意思?这不是正则表达式的东西,对吧?我也不清楚为什么我应该用“^”开始一个 RewriteRule。你能推荐任何关于这些问题的好的文档页面吗?
    • @Fabbio wiki.apache.org/httpd/RewriteRule ; %1 表示来自RewriteCond 语句的匹配组,$1 表示来自RewriteRules 的匹配组。在 rewriterule 中使用 ^ 时,我完全忽略了 url(因为我正在处理原始请求,所以无论如何都不需要它)。
    猜你喜欢
    • 1970-01-01
    • 2015-01-13
    • 2018-04-28
    • 2012-05-22
    • 1970-01-01
    • 2015-04-17
    • 2011-08-25
    • 1970-01-01
    相关资源
    最近更新 更多