我想用.htaccess重定向以下网址:
/ books / garden to / garden-books
/ movies / dvd-comedy to / dvd / comedy
/ music-soundtracks / new to / music / soundtracks
任何人都知道怎么做?
答案 0 :(得分:3)
如果你想要一个更简单的解决方案,你也可以试试这个:
Redirect 301 /books/garden /garden-books
Redirect 301 /movies/dvd-comedy /dvd/comedy
Redirect 301 /music-soundtracks/new /music/soundtracks
旧网址首先是“重定向301”,然后是新网址。
答案 1 :(得分:1)
将此添加到您网站的根目录中的.htaccess文件中。
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+(books)/(garden)\s [NC]
RewriteRule ^ /%2-%1 [R=301,L]
RewriteRule ^(garden)-(books)/?$ /$2/$1 [NC,L]
RewriteCond %{THE_REQUEST} \ /+movies/(dvd)-(comedy)/?[\s?] [NC]
RewriteRule ^ /%1/%2 [R=301,L]
RewriteRule ^(dvd)/(comedy)/?$ /movies/$1-$2 [NC,L]
RewriteCond %{THE_REQUEST} \ /+(music)-(soundtracks)/new/?[\s?] [NC]
RewriteRule ^ /%1/%2 [R=301,L]
RewriteRule ^(music)/(soundtracks)/?$ /$1-$2/new [NC,L]
是的,这是可能的,但您需要考虑这一点:如果您的实际网址是/page.php?var=val
,而您的漂亮网址是/page/var/val
,则上述规则会确保不仅漂亮的版本/page/var/val
有效,但如果有人试图访问实际的/page.php?var=val
,他们也会被重定向回/page/var/val
。
因此,如果您只对解析漂亮网址感兴趣,则只能使用以下内容。
RewriteEngine On
RewriteRule ^(garden)-(books)/?$ /$2/$1 [NC,L]
RewriteRule ^(dvd)/(comedy)/?$ /movies/$1-$2 [NC,L]
RewriteRule ^(music)/(soundtracks)/?$ /$1-$2/new [NC,L]