.htaccess将URL重定向到特定模式

时间:2014-12-10 23:33:41

标签: .htaccess mod-rewrite url-rewriting

假设我有一些网址,如下所示:

  • site.com/html/photographers-4.cfm?county=test1
  • site.com/html/photographers-5.cfm?county=test2&speciality=test3
  • site.com/html/photographers-9.cfm?address4=test4&county=test5
  • site.com/html/photographers-10.cfm?county=test6&speciality=test7&address4=test8

其中 testX (...)可能相同或不同。

我如何能够重写这些内容以使URL更引人注目?

  • site.com/test1-photographers
  • site.com/test2-tes33-photographers
  • site.com/test4-test5-photographers
  • site.com/test7-test8-photographers

县,专业,地址1,地址2,地址3,地址4 只是示例。还有更多。

假设我有这个特定的链接:

  

photographers.co.uk/html/photographers-10.cfm?county=New   汉普郡和专业=婚礼&地址4 =南朴茨茅斯

  

photographers.co.uk/html/photographers-10.cfm?county=New   York& special = Wedding& address4 =朴茨茅斯新城

我喜欢当它找到像

这样的单词时的规则
  

南朴茨茅斯

  

朴茨茅斯新城

遵循我的规则并使其看起来像:

  

南 - 朴次茅斯

  

朴茨茅斯新城

我的.htaccess是这样的:

 Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)(?:\+|%20|\s)+(.+?)\sHTTP [NC]
RewriteRule ^ /%1-%2 [L,NE,R=302]
RewriteRule ^([_&/A-Za-z0-9\s]+)-([&/A-Za-z0-9\s]+)-([&/A-Za-z0-9\s]+)-Photographers?$ html/photographers-10.cfm?county=$1&speciality=$2&address4=$3&rwtn [NC,L]
RewriteCond %{QUERY_STRING} !rwtn
RewriteCond %{QUERY_STRING} ^county=(.*)&speciality=(.*)&address4=(.*)
RewriteRule ^html/photographers-10\.cfm$ http://www.photographers.co.uk/%1-%2-%3-Photographers? [R=301,L]




RewriteRule ^([_&/A-Za-z0-9\s]+)-Photographers/?$ html/photographers-4.cfm?county=$1&rwtn [NC,L]
RewriteCond %{QUERY_STRING} !rwtn
RewriteCond %{QUERY_STRING} ^county=(.*)
RewriteRule ^html/photographers-4\.cfm$ http://www.photographers.co.uk/%1-Photographers? [R=301,L]



RewriteRule ^([_&/A-Za-z0-9]+)-([&/A-Za-z0-9]+)-Photographers?$ html/photographers-5.cfm?county=$1&speciality=$2&rwtn [NC,L]
RewriteCond %{QUERY_STRING} !rwtn
RewriteCond %{QUERY_STRING} ^county=(.*)&speciality=(.*)
RewriteRule ^html/photographers-5\.cfm$ http://www.photographers.co.uk/%1-%2-Photographers? [R=301,L]



RewriteRule ^Canary-Wharf-London-Photographer/?$ html/photographers-9.cfm?address4=Canary-Wharf&county=London&rwtn [NC,L]
RewriteRule ^City-of%2520London-London-Photographer/?$ html/photographers-9.cfm?address4=City-of%20London&county=London&rwtn [NC,L]
RewriteRule ^East-London-London-Photographer/?$ html/photographers-9.cfm?address4=East-London&county=London&rwtn [NC,L]
RewriteRule ^Kingston-upon%2520Thames-London-Photographer/?$ html/photographers-9.cfm?address4=Kingston-upon Thames&county=London&rwtn [NC,L]
RewriteRule ^NOTTING-HILL-London-Photographer/?$ html/photographers-9.cfm?address4=NOTTING-HILL&county=London&rwtn [NC,L]
RewriteRule ^South-Woodford-London-Photographer/?$ html/photographers-9.cfm?address4=South-Woodford&county=London&rwtn [NC,L]
RewriteRule ^Stamford-Brook-London-Photographer/?$ html/photographers-9.cfm?address4=Stamford-Brook&county=London&rwtn [NC,L]

RewriteRule ^([_&/A-Za-z0-9\s]+)-([&/A-Za-z0-9\s]+)-Photographer/?$ html/photographers-9.cfm?address4=$1&county=$2&rwtn [NC,L]
RewriteCond %{QUERY_STRING} !rwtn
RewriteCond %{QUERY_STRING} ^address4=(.*)&county=(.*)
RewriteRule ^html/photographers-9\.cfm$ http://www.photographers.co.uk/%1-%2-Photographer/? [R=301,L]

1 个答案:

答案 0 :(得分:0)

您可以将此规则用作您的第一条规则,以用连字符替换所有空格。空格可以在查询字符串中和/或REQUEST_URI中。

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)(?:\+|%20|\s)+(.+?)\sHTTP [NC]
RewriteRule ^ /%1-%2 [L,NE,R=302]

这将重定向:

  

/html/photographers-10.cfm?county=New York&speciality=Wedding&address4=Portsmouth New City

为:

  

/html/photographers-10.cfm?county=New-York&speciality=Wedding&address4=Portsmouth-New-City

请注意,所有空格都已转换为最终URI中的连字符