簡體   English   中英

htaccess只有在沒有提供參數時才將一個域重定向到另一個域?

[英]htaccess to redirect one domain to another only if no parameters are provided?

我有一個域oldexample.com,我想只在沒有給出參數的情況下將訪問該域的用戶重定向到newexample.com。 在域提供參數的情況下,它應該重定向到其他地方 - 因此,oldexample.com/?id=5不應該重定向到newexample.com,而是我想將它重定向到newexample.com中的某個內部目錄,比如說到newexample.com/dir/5。

以下是我希望重定向如何工作的摘要:

oldexample.com -> newexample.com
oldexample.com/?id=3 -> newexample.com/dir/3
oldexample.com/index.php?id=6 -> newexample.com/dir/6

那么我如何在htaccess中編寫RewriteRules來完成這項工作呢?

嘗試:

RewriteEngine On

# for /
RewriteCond %{HTTP_HOST} ^oldexample.com$ [NC]
RewriteCond %{QUERY_STRING} !id=
RewriteRule ^$ http://newexample.com/ [L,R=301]

# for /?id=###
RewriteCond %{HTTP_HOST} ^oldexample.com$ [NC]
RewriteCond %{QUERY_STRING} ^id=([0-9]+)
RewriteRule ^(index.php)?$ http://newexample.com/dir/%1? [L,R=301]

最后一條規則與//index.php匹配。 這些規則必須位於oldexampl.com文檔根目錄中的htaccess文件中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM