htaccess的重定向URL到特定模式 [英] .htaccess redirect URL to specific pattern

查看:147
本文介绍了htaccess的重定向URL到特定模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假如我有一些网址,看起来像这样:

  • 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,地址,地址3,地址4 只是例子。可能有更多的。

假如我有这个特殊的链接:

  

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

  

photographers.co.uk/html/photographers-10.cfm?county=New   纽约和放大器;专业=婚礼及放大器;地址4 =朴茨茅斯新城

我想要的规则,当它发现一个字像

  

南朴茨茅斯

  

朴茨茅斯新城

要按照我的规则,使它看起来像:

  

南朴茨茅斯

  

朴茨茅斯新城

我的.htaccess这件事情是这样的:

 选项+了FollowSymLinks -MultiViews
    #开启mod_rewrite的上
    RewriteEngine叙述上
    的RewriteBase /
的RewriteCond%{THE_REQUEST} ^ [AZ] {3,} \ S /+(.*?)(?:\+ | 20%|?\ S)+(+)\ SHTTP [NC]
重写规则^ /%1%2 [L,NE,R = 302]
重写规则^([_&放; / A-ZA-Z0-9 \ S] +) - ([安培; / A-ZA-Z0-9 \ S] +) - ([安培; / A-ZA-Z0-9 \ S] +) - 摄影师$ HTML /摄影师-10.cfm 1县= $&放大器;?专业= $ 2及地址4 = $ 3及rwtn [NC,L]
的RewriteCond%{QUERY_STRING}!rwtn
的RewriteCond%{QUERY_STRING} ^县=&AMP(*);专业=&AMP(*)(。*)的地址4 =
重写规则^ HTML /摄影师-10 \ .CFM $ http://www.photographers.co.uk/%1-%2-%3-Photographers? [R = 301,L]




重写规则^([_&放; / A-ZA-Z0-9 \ S] +) - 摄影师/ $ HTML /摄影师,4.cfm县= $ 1安培;?rwtn [NC,L]
的RewriteCond%{QUERY_STRING}!rwtn
的RewriteCond%{QUERY_STRING} ^县=(。*)
重写规则^ HTML /摄影师-4 \ .CFM $ http://www.photographers.co.uk/%1-Photographers? [R = 301,L]



重写规则^([_&放; / A-ZA-Z0-9] +) - ([安培; / A-ZA-Z0-9] +) - ?摄影师$ HTML /摄影师-5.cfm 1县= $&放大器;专业= $ 2及rwtn [NC,L]
的RewriteCond%{QUERY_STRING}!rwtn
的RewriteCond%{QUERY_STRING} ^县=&AMP(*)(。*)专业=
重写规则^ HTML /摄影师-5 \ .CFM $ http://www.photographers.co.uk/%1-%2-Photographers? [R = 301,L]



重写规则^加那利码头 - 伦敦 - 摄影师/ $ HTML /摄影师,9.cfm地址4 =加那利码头及放大器;?县=伦敦和放大器; rwtn [NC,L]
重写规则^%市的2520London  - 伦敦 - 摄影师/ $ HTML /摄影师,9.cfm地址4 =%市的20London和放大器;?县=伦敦和放大器; rwtn [NC,L]
重写规则^东伦敦 - 伦敦 - 摄影师/ $ HTML /摄影师,9.cfm地址4 =东伦敦和放大器;?县=伦敦和放大器; rwtn [NC,L]
重写规则^金斯顿%2520Thames  - 伦敦 - 摄影师/ $ HTML /摄影师,9.cfm地址4 =金斯敦在泰晤士和放大器;?县=伦敦和放大器; rwtn [NC,L]
?重写规则^ NOTTING-HILL  - 伦敦 - 摄影师/ $ HTML /摄影师,9.cfm地址4 = NOTTING-HILL&放大器;县=伦敦和放大器; rwtn [NC,L]
重写规则^南伍德福德 - 伦敦 - 摄影师/ $ HTML /摄影师,9.cfm地址4 =南伍德福德和放大器;?县=伦敦和放大器; rwtn [NC,L]
重写规则^斯坦福布鲁克 - 伦敦 - 摄影师/ $ HTML /摄影师,9.cfm地址4 =斯坦福 - 布鲁克和放大器;?县=伦敦和放大器; rwtn [NC,L]

重写规则^([_&放; / A-ZA-Z0-9 \ S] +) - ([安培; / A-ZA-Z0-9 \ S] +) - 摄影师/ $ HTML /摄影师,9.cfm?地址4 = $ 1安培;县2 = $&放大器; rwtn [NC,L]
的RewriteCond%{QUERY_STRING}!rwtn
的RewriteCond%{QUERY_STRING} ^地址4 =&AMP(*)(。*)县=
重写规则^ HTML /摄影师-9 \ .CFM $ http://www.photographers.co.uk/%1-%2-Photographer/? [R = 301,L]
 

解决方案

您可以使用此规则为您的第一个规则按连字符替换所有的空间。空间可以是在查询字符串和/或在REQUEST_URI

 的RewriteCond%{THE_REQUEST} ^ [AZ] {3,} \ S /+(.*?)(?:\+ | 20%| \ S)+(+ ?)\ SHTTP [NC]
重写规则^ /%1%2 [L,NE,R = 302]
 

这将重定向:

  

/html/photographers-10.cfm?county=New纽约和放大器;专业=婚礼及放大器;地址4 =朴茨茅斯新城

  

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

注意,所有的空间都被转换在最后的URI来连字符

Supposing I have some URLs, looking like this:

  • 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

where testX (...) may be the same or may differ.

How would I be able to rewrite each of these so that the URL will be more eye-catching ?

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

county,speciality,address1,address2,address3,address4 are just examples. There could be more.

Supposing I have this particular link:

photographers.co.uk/html/photographers-10.cfm?county=New Hampshire&speciality=Wedding&address4=South Portsmouth

or

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

I'd like the rule that when it finds a word like

South Portsmouth

or

Portsmouth New City

to follow my rules and to make it look like:

South-Portsmouth

or

Portsmouth New City

My .htaccess it's something like this:

 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]

解决方案

You can use this rule as your very first rule to replace all your spaces by hyphen. Spaces can be in query string and/or in REQUEST_URI.

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

This will redirect:

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

to:

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

Note all spaces have been converted to hyphen in final URI

这篇关于htaccess的重定向URL到特定模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆