2011-12-27 98 views
4

我試圖使用htaccess重定向到一個文件,但無法獲取圖像顯示。htaccess重定向但排除圖像

我想重定向到mydomain.com或www.mydomain.com任何要求www.mydomain.com/test.html

這是我的htaccess文件的內容:

# Turn rewriting on 
Options +FollowSymLinks 
RewriteEngine On 
RewriteCond %{REQUEST_URI} !=/test.html 
RewriteCond %{REQUEST_URI} !=*\.png$ [NC] 
RewriteRule .* /test.html 

我在我的test.html文件放在這行代碼:

<img src="image.png" alt="My Image"> 

但圖像不顯示在所有的,我只是得到一個破碎的形象。我也嘗試使用圖像的絕對路徑,同樣的事情。

回答

12

嘗試更換.htaccess文件有以下

# Turn rewriting on 
Options +FollowSymLinks 
RewriteEngine On 
RewriteBase/

#if the request does not end with test.html or is not a .png 
RewriteCond %{REQUEST_URI} !(test\.html|\.png)$ [NC] 
# Rewrite it to test.html 
RewriteRule .* /test.html [L] 
+0

真棒,就像一個魅力!非常感謝你! – user1078494 2011-12-27 21:45:42

+0

+1更簡潔。 – 2011-12-27 21:48:23

2

嘗試:

RewriteCond %{REQUEST_URI} !\.png$ [NC] 

不熟悉!.前綴。檢查RewriteCond Docs

+0

是啊,對不起,我不知道是錯字如何到達那裏的。但我的htaccess文件中的代碼是正確的,我只是編輯了我的第一篇文章。 – user1078494 2011-12-27 21:39:29

+0

從RewriteCond文檔中,當使用'='時,模式被視爲純文本,所以這可能不起作用 – 2011-12-27 21:44:36

+0

我已經刪除了'= *' – 2011-12-27 21:47:43

4

這將排除各種圖像

#if the request does not end with .gif,.png or jpg 
RewriteCond %{REQUEST_URI} !(\.gif|\.jpg|\.png)$ [NC] 
RewriteRule ^index.html$ index.php [NC]