【问题标题】:Optional parameters in URL - rewrite ruleURL 中的可选参数 - 重写规则
【发布时间】:2011-10-22 04:36:45
【问题描述】:

我不知道“可选参数”这个词是否正确描述了我的情况。这是我需要的。

我为 URL 重定向编写了以下规则:

RewriteRule ^product/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ product/?sid=$2&pid=$3&title=$1&src=$4 [NC,L]

基本上,这将重定向类似

http://localdomain.com/product/Golf-Bats/abc123/def456/stringy

类似

http://localdomain.com/product/?sid=abc123&pid=def456&title=Golf-Bats&src=stringy

我想要做的是编写一个规则,该规则接受附加/可选/潜在无限数量的参数( // 类型构造)但仍重定向到相同的 URL。

表示以下网址:

http://localdomain.com/product/Golf-Bats/abc123/def456/stringy
http://localdomain.com/product/Golf-Bats/abc123/def456/stringy/rand1
http://localdomain.com/product/Golf-Bats/abc123/def456/stringy/rand1/rand2
http://localdomain.com/product/Golf-Bats/abc123/def456/stringy/rand1/rand2/rand3
etc.

都应该指向网址

http://localdomain.com/product/?sid=abc123&pid=def456&title=Golf-Bats&src=stringy

有什么想法吗?

【问题讨论】:

    标签: apache .htaccess mod-rewrite apache2.2


    【解决方案1】:

    使用这条规则——它将使第 6 个和更多的路径段成为“可选”:

    RewriteRule ^product/([a-z0-9\-]+)/([a-z0-9\-]+)/([a-z0-9\-]+)/([a-z0-9\-]+)(/.*)?$ product/?sid=$2&pid=$3&title=$1&src=$4 [NC,L]
    

    此规则会将所有这些 URL 视为相同(将重定向到相同的 URL):

    http://localdomain.com/product/Golf-Bats/abc123/def456/stringy
    http://localdomain.com/product/Golf-Bats/abc123/def456/stringy/rand1
    http://localdomain.com/product/Golf-Bats/abc123/def456/stringy/rand1/rand2
    http://localdomain.com/product/Golf-Bats/abc123/def456/stringy/rand1/rand2/rand3
    
    1. 我已将 A-Za-z 替换为模式中的 a-z,因为您已经有 [NC] 标志(忽略大小写)。

    2. 请记住,从 SEO 的角度来看,这种 URL 通常不好 - 我强烈建议使用 <link rel="canonical" href="PROPER_URL" /> 指定正确的 URL,以避免搜索引擎重复内容的惩罚:

    3. “可选”部分将丢失/不会按要求传递到新 URL。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-18
      • 1970-01-01
      • 2014-08-14
      • 2016-08-18
      • 2016-02-29
      • 1970-01-01
      • 2011-06-21
      • 2013-11-10
      相关资源
      最近更新 更多