2011-10-28 16 views
0

這裏是我的.htaccess代碼的.htaccess和robots.txt的重寫URL如何

RewriteEngine on 
RewriteRule (.*) index.html 

問題:當訪問mydomain.com/robots.txt然後再次頁面重定向到index.html的
要求:

if(url contain robots.txt) Then 
    redirect to mydomain.com/robots.txt 
else 
    redirect to index.html 

回答

4

試試這個:

RewriteEngine on 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule (.*) index.html 

基本上,這兩個RewriteCond告訴Apache重寫URL 只有請求的文件(-f)或目錄(-d)不存在(在!作爲否定)。

另外,如果你需要它只是爲robots.txt您可以使用類似:

RewriteCond %{REQUEST_URI} !^/robots.txt$ 

,而不是上述兩個RewriteCond

+0

你能給我你的聯繫方式,臉書,Skype或任何電子郵件。 –

+0

%{REQUEST_URI}和RewriteCond –

+0

@Wasim的含義基本上,RewriteCond就像是在說「if CONDITION then」。 REQUEST_URI是實際傳遞的URI:http://domain.tld/robots.txt。看看:http://httpd.apache.org/docs/current/mod/mod_rewrite.html –