Apache-2.2

Apache/htaccess 中的緊湊重定向。如何?安__起碼__一個ntCod我__問號_______一種n和一世和G一種n噸C這d一世nG問你和s噸一世這nAn Elegant Coding Question

  • December 19, 2010

在 htaccess 中設置我的重定向時,當它們都轉到同一個首頁時,我無法設置/組合各種域。我可以將它們分開,但這並不整潔/優雅。如何重寫第三段以使其有效?

RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP_HOST} ^website.de$    // works fine
RewriteRule ^$ de/home [R=301,L]

RewriteCond %{HTTP_HOST} ^website.fr$    // works fine
RewriteRule ^$ fr/home [R=301,L]

RewriteCond %{HTTP_HOST} ^website.com$    // doesnt work well
RewriteCond %{HTTP_HOST} ^website.org$    // doesnt work well
RewriteCond %{HTTP_HOST} ^website.net$    // doesnt work well
RewriteRule ^$ en/home [R=301,L]
RewriteCond %{HTTP_HOST} ^website\.(com|org|net)$
RewriteRule ^$ en/home [R=301,L]

也是編寫所有這些的更好方法:

RewriteCond %{HTTP_HOST} ^website\.(fr|de)$
RewriteRule ^$ %1/home [R=301,L]

RewriteCond %{HTTP_HOST} ^website\.(com|org|net)$
RewriteRule ^$ en/home [R=301,L]

只是為了另一個說明,您可以使用將多個 RewriteCond 組合為“或”條件

$$ OR $$,例如:

RewriteCond ... [OR]
RewriteCond ...
RewriteRule ...

引用自:https://serverfault.com/questions/214211