问题
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]
回答1:
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
来源:https://stackoverflow.com/questions/27412854/htaccess-redirect-url-to-specific-pattern