2013-03-16 67 views
0

我想讓我的子域的訪問者僅在direclty時使用特定的子域鏈接格式。我知道這也會阻止SE,但我不希望該子域被索引。htaccess在子域跳過重定向規則

允許的鏈接應該是這樣的:

subdomain.maindomain.com/aaa/bbb/ccc

,須重寫本:

子域。 maindomain.com/index.php?a=aaa & b = bbb & c = ccc

寄託都這是什麼形式的不和從空或外部引用降臨的人,去主域和不變量:

maindomain.com/

我與所有類型的配置試過了,我最後在子域文件夾.htaccess文件看起來是這樣的:

RewriteEngine on 
RewriteRule ^/?(\w{3})/(\w{3})/(\w{3})$ index.php?a=$1&b=$2&c=$3 [S=1,L] 

RewriteCond %{HTTP_REFERER} !^http://(www\.)?maindomain\.com [NC] 
RewriteRule ^(.*)$ http://domain\.com/ [L] 

但它仍然沒有做我想要的東西,它重定向還請求允許的形式向主域名的子域,它也增加了增值經銷商的請求主域名,轉到此頁

maindomain.com/?a=aaa & B = BBB & C = CEE

你能幫我上面所規定的條件。

第二個問題,關於性能:我明顯可以使用PHP進行此驗證/重定向,您認爲哪種方法更有效?

謝謝!

回答

1

如果我理解正確的話你的邏輯,請試試這個:

RewriteEngine on 
RewriteBase/

# Don't do any more rewrites if on index.php 
# Note that you can add the HTTP_HOST condition here if you only want it to be active for the subdomain 
RewriteRule ^index\.php$ - [L] 

# If on subdomain then check that the referer is from main domain and attempt to match a regex 
# If we find a match then ignore the next rule that rewrites subdomain to domain.com 
# Basically this is like an awkward if-else statement.. 
# ================  

RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$ 
RewriteCond %{HTTP_REFERER} ^http://(www\.)?domain\.com [NC] 
# Rewrites /aaa/bbb/ccc to /index.php?a=aaa&b=bbb&c=ccc 
RewriteRule ^(\w{3})/(\w{3})/(\w{3})$ index.php?a=$1&b=$2&c=$3 [S=1,L] 

# Redirect all requests from subdomain to domain.com by default 
# ================ 

RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$ 
# Add the trailing question mark to delete all query parameters 
RewriteRule .* http://domain.com/? [L] 
+0

謝謝,但沒有,仍然無法正常工作。 直接訪問http://subdomain.domain.com/aaa/bbb/cee仍然被重定向到http://domain.com/ – user2176640 2013-03-16 10:05:35

+0

什麼不起作用?你是否從subdomain.domain.com重寫到domain.com?請注意,我已經更新了很多次代碼。 – kjetilh 2013-03-16 10:18:13

+0

@ user2176640請參閱我的更新回答。我們必須在頂部添加'RewriteRule^index \ .php $ - [L]'指令,因爲'[L]'標誌並不總是表明停止所有進一步的重寫處理。 – kjetilh 2013-03-16 10:29:08

0

你是八九不離十。您只需要幾個附加部件

  • 標誌S=1不傷害,但也不是必需的。
  • 將引用條件移至第一條規則並將其倒置。您可能也想要允許任何子域。
  • 增加一個否定條件,以將index.php從重定向到主域。
  • 您不會在第二條規則的替換中使用原始請求,因此您無需捕獲它。
  • 附加一個問號?以防止參數被轉發到主域。
  • 添加一個R標誌,如果您希望重定向客戶端,而不僅僅重寫請求。

所有部件一起

RewriteEngine on 

RewriteCond %{HTTP_REFERER} ^http://(.+\.)?maindomain\.com [NC] 
RewriteRule ^(\w{3})/(\w{3})/(\w{3})$ index.php?a=$1&b=$2&c=$3 [L] 

RewriteCond %{REQUEST_URI} !$index\.php$ 
RewriteRule^http://domain\.com/? [R,L] 

至於性能,如果你重寫或只是重定向的.htaccess,它甚至碰到一些PHP腳本之前的工作已經完成。所以,我認爲最好用.htaccess來做到這一點。

+0

謝謝。沒有格式爲subdomain.maindomain.com/aaa/bbb/ccc的引薦來源的網址仍會重定向到maindomain.com。我開始認爲可能存在其他配置錯誤,也許在apaches虛擬主機中?任何ideeas? – user2176640 2013-03-17 09:21:46

+0

@ user2176640我想,這是你想要的。你寫道:「假設沒有這種形式,並且來自空的或外部的推薦人應該到達主要領域並且沒有變量」 – 2013-03-17 11:51:22