简体   繁体   中英

htaccess url masking mod rewrite

I have done some URL masking and it all works very nicely. I have one issue that I am trying to resolve now:

RewriteRule ^(.*)/(.*)/clubs/(.*)/$ teams.php?competition=$1&season=$2&teamid=$3

with the above I can access the same page 2 ways, www.domain.com/premier-league/2010-2011/arsenal/ and www.domain.com?competition=premier-league&season=2010-2011&teamid=arsenal

is there a way in my rewrite rule I can redirect the URL (301 ideally) is someone does it through the untidy way " www.domain.com?competition=premier-league&season=2010-2011&teamid=arsenal "?

Thanks in advance

If you add another rewrite rule after this one that matches your 'inverse' pattern, and mark both as the [L]ast rule, that might work. I first rewrite the url to include the query string, and [C]hain that one to the next rule. After that we [R]edirect the browser.

RewriteRule ^(.)/(.)/clubs/(.*)/$ teams.php?competition=$1&season=$2&teamid=$3 [L]
RewriteRule ^(.+) $1%{QUERY_STRING} [C]
RewriteRule ^teams.php?competition=([a-zA-Z0-9]+)&season=([a-zA-Z0-9]+)&teamid=([a-zA-Z0-9]+)$ $1/$2/clubs/$3/ [R=301,L]

Note I haven't tested this or the regex of the second rule. Might need to adjust the character ranges a bit. Also I haven't tested the query string rewrite in the second rule.

Edit: See here for some common use cases of mod_rewrite : http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM