2009-09-07 15 views
0

我有一個應用程序,它大部分仍在開發中,這就是爲什麼我需要阻止所有頁面上的訪問,但不是其中兩個。重寫規則存在問題

假設我有a.php所有的請求將被重定向到這裏除了b.php。

所以,我覺得應該有3個規則:

1st: when a.php and b.php are requested they should be visible to user, 

2nd: if anything other than those two is requested, 
    it should be redirected to a.php. 

3rd: for external css,javascript and image files 
    there should be an access rule 

因爲我沒有與服務器管理太多的經驗,我認爲我完全失去了:)

這是我的嘗試:

RewriteBase/
RewriteCond %{REQUEST_FILENAME} !^/b.php 
RewriteRule !\.(js|ico|txt|gif|jpg|png|css)$ a.php 
+0

對於這兩個答案+1我選擇了Gumbo的答案,因爲我更瞭解規則,謝謝你的答案。 – Sinan 2009-09-08 13:15:37

回答

2

在實踐中你會換第二個和第三個規則是你的第二個規則是默認路由:

# 1st 
RewriteRule ^(a\.php|b\.php)$ - [L] 

# 3rd 
RewriteRule \.(js|ico|txt|gif|jpg|png|css)$ - [L] 

# 2nd 
RewriteRule !^a\.php$ a.php [L] 

的subsitution -意味着該URI沒有改變。

+0

對我來說看起來不錯。不要忘記'RewriteEngine on'。此外,定義這些規則的上下文(配置根,.htaccess)也很重要。 – zedoo 2009-09-07 20:58:56

2

我不確定你對#3的含義,但是從你正在嘗試的方式看,我想你的意思是,所有js,ico,...都將重定向到a.php。是對的嗎?如果是這樣,那意味着a => a.php(帶參數),b => b.php(帶參數)和其他=> a.php。所以,試試這個:

RewriteBase/
RewriteRule ^a\.php(.*) /a.php$1 
RewriteRule ^b\.php(.*) /b.php$1 
RewriteRule ^.*   /a.php 

但是,如果你指的是所有這些腳本和媒體文件都可以訪問正常,試試這個:

RewriteBase/
RewriteRule ^a\.php(.*)       /a.php$1 
RewriteRule ^b\.php(.*)       /b.php$1 
RewriteRule ^(.*)\.(js|ico|txt|gif|jpg|png|css)$ /$1.$2 

基本上重寫規則的正則表達式和$ 1, $ 2,...是匹配字符串組(用「(」和「)」包裹的那些)。

希望得到這個幫助。