2012-07-25 192 views
1

我一直試圖讓這個權利現在1或2個小時,尋找類似的問題,並嘗試解決方案。這可能是答案真的很明顯的問題之一。.htaccess重定向404錯誤

我試圖重定向大家誰正在訪問domain.com/title/ 任何domain.com/title/ anotherpage PHP腳本,但它給我一個404錯誤時我嘗試訪問該頁面。

我在我的htaccess文件使用此代碼:

RewriteEngine on 
RewriteRule ^/plugins/(.*)$ /yourscript.php?plugin=$1 [L] 

這裏是我的.htaccess文件的完整副本。

ErrorDocument 404 /error/404.php 

#Prevent full listing of files 
Options -Indexes 

#Redirect plugins 
<IfModule mod_rewrite.c> 
RewriteEngine on 
RewriteRule ^/plugins/(.*)$ /foobar.php?plugin=$1 [NC,L] 
</IfModule> 

#Hide .php extension 
<IfModule mod_rewrite.c> 
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php [NC,L] 
</IfModule> 

#compress 
AddOutputFilterByType DEFLATE text/plain 
AddOutputFilterByType DEFLATE text/html 
AddOutputFilterByType DEFLATE text/xml 
AddOutputFilterByType DEFLATE text/css 
AddOutputFilterByType DEFLATE application/xml 
AddOutputFilterByType DEFLATE application/xhtml+xml 
AddOutputFilterByType DEFLATE application/rss+xml 
AddOutputFilterByType DEFLATE application/javascript 
AddOutputFilterByType DEFLATE application/x-javascript 

我已經嘗試將foobar.php腳本放在根文件夾和文件夾名稱插件中。

非常感謝您的幫助。

+0

你得到的錯誤信息是什麼?一個標準的404或類似「另外,嘗試使用ErrorDocument時遇到404 Not Found錯誤」?還要注意,如果我沒有弄錯,'ErrorDocument 404/error/404.php'將指向'domain.com/error/404.php'(這是你的意圖嗎?)。還請嘗試通過(暫時)刪除您的htaccess中不相關的行來找出錯誤。 – Spooky 2012-07-25 21:59:09

+0

@Spooky我得到一個正常的404錯誤。否「另外...」。 ErrorDocument的位置是打算的,並顯示我的自定義錯誤頁罰款。我也試過我的代碼的第一個片段,但它仍然給我錯誤。 – Mastergalen 2012-07-25 22:05:46

+0

「顯示我的自定義錯誤頁面」 - 然後...我想我不理解你的問題/目標是什麼。如果這與'RewriteRule'有關,那麼我就會退縮,因爲我不知道。 – Spooky 2012-07-25 22:11:10

回答

2

看起來一切都在正確的地方,但你可能想嘗試在你的正則表達式中去除前導斜槓(或者至少使其可選):

RewriteRule ^/?plugins/(.*)$ /yourscript.php?plugin=$1 [L] 

在htaccess文件,阿帕奇移除了在將它傳遞給htaccess文件中的規則之前,該URI的前綴(前導斜槓)。


domain.com/plugins/ 任何得到了正確的重定向。不過,domain.com/ 插件也被定向到該腳本。相反,我想擁有它,使之指向到一個文件夾名稱的index.php文件「插件」

然後,你需要更改上面的規則:

RewriteRule ^/?plugins/(.+)$ /yourscript.php?plugin=$1 [L] 

(注*更改爲+以匹配至少1個字符)。只要index.php是一個有效的索引,你可能不需要添加任何規則,這意味着如果你只是去/plugins/ apache將自動服務於index.php文件。

+0

謝謝,這個作品很棒。 _domain.com/plugins/**whatever**_被正確重定向。但是,_domain.com/**plugins**_也被定向到腳本。相反,我想擁有它,以便它指向一個索引。PHP文件中的文件夾名稱「插件」。 – Mastergalen 2012-07-26 12:23:11

+0

@mastergalen看到我上面的編輯。 – 2012-07-26 17:19:18

+0

嗯,它仍然匹配domain.com/plugins並將其重定向到yourscript.php。我刪除了規則,並去了插件文件夾,它爲我服務index.php,所以它是一個有效的索引,只是一個htacess規則問題。 – Mastergalen 2012-07-27 11:22:12