2012-09-13 46 views
0

使用.htaccess,我想將不存在的文件重定向到控制器頁面,並重寫存在的.php文件擴展名爲一個.html擴展名。如果文件存在並且是.html頁面,我希望它保持不變。每次我嘗試將重寫規則從.php注入到.html時,我似乎都搞亂了重定向到控制器頁面。所以我不知道該從哪裏開始:如果文件存在,則重寫擴展名,如果文件不存在則重定向,使用htaccess

Options -Indexes 
Options +FollowSymLinks 
DirectoryIndex index.php 
ErrorDocument 404 /404.php 

<IfModule mod_rewrite.c> 
    RewriteEngine on 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_URI} !=/favicon.ico 
    RewriteRule ^(.*)$ mycontroller.php [L,QSA] 
</IfModule> 

任何幫助,我將不勝感激。

編輯

我似乎找到了大部分答案here的(但我必須離開了ReweriteBse或者它不工作)。最大的問題是,現在,我現有的.html文件不起作用,它只會提供帶有.html擴展名的.php文件,並將其他所有內容指向控制器。現有的.html文件轉到我的404頁面。我想知道如何保持現有的.html文件不變。我的新代碼如下:

RewriteEngine on 

    RewriteCond %{THE_REQUEST} (.*)\.php 
    RewriteRule ^(.*)\.php $1.html [R=301,L] 

    RewriteCond %{THE_REQUEST} (.*)\.html 
    RewriteRule ^(.*)\.html $1.php [L] 

    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_URI} !=/favicon.ico 
    RewriteRule ^(.*)$ mycontroller.php [L,QSA] 

回答

0

我確定有更好的方法來做到這一點,但以下工作適合我的需求。提供的其他答案似乎沒有工作,儘管我嘗試。

Options -Indexes 
Options +FollowSymLinks 
DirectoryIndex index.php 
ErrorDocument 404 /404.php 

<IfModule mod_rewrite.c> 
    RewriteEngine on 

    # Check if .html file already exists -- if so, do nothing 
    RewriteCond %{REQUEST_FILENAME} -f 
    RewriteCond %{THE_REQUEST} (.*)\.html 
    RewriteRule ^.*$ - [NC,L] 

    # Check if .php file already exists -- if so, rewrite extension to .html 
    RewriteCond %{REQUEST_FILENAME} -f 
    RewriteCond %{THE_REQUEST} (.*)\.php 
    RewriteRule ^(.*)\.php $1.html [R=301,L] 

    RewriteCond %{THE_REQUEST} (.*)\.html 
    RewriteRule ^(.*)\.html $1.php [L] 

    # All else goes to the controller page 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_URI} !=/favicon.ico 
    RewriteRule ^(.*)$ mycontroller.php [L,QSA] 
</IfModule> 
0

嘗試這種(I添加了一個重寫條件以避免無限循環B yadding參數r = 0和測試,如果它存在):

RewriteEngine on 

    RewriteCond %{QUERY_STRING} ^(.*&)?r=0(&.*)?$ 
    RewriteRule ^(.*)\.php$ $1.html [L,R=301,QSA] 

    RewriteCond %{REQUEST_FILENAME} ^(.*)\.html$ 
    RewriteCond %1.php -f 
    RewriteRule ^(.*)\.html$ $1.php?r=0 [L,QSA] 

    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_URI} !=/favicon.ico 
    RewriteRule ^(.*)$ mycontroller.php [L,QSA] 
+0

感謝您的快速響應!我把它放進去,現在我得到了「沒有指定輸入文件」。除了存在的.php文件之外的所有東西(我必須以.php格式輸入它們)。 – seaofinformation

+0

好吧,也許我的第五行是造成這個問題,讓我想想吧 – Oussama

+0

我應該有資格 - 我正在上傳.htaccess不正確。現在我已經正確上傳了,但似乎沒有任何變化。如果我輸入.html,它不會提供相應的.php頁面,如果它存在,只會轉到404錯誤。 – seaofinformation

1

嘗試:

<IfModule mod_rewrite.c> 
    RewriteEngine on 

    # If a request for a php file is made, check that it's actually a php file then redirect the browser 
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*?)\.php($|\) 
    RewriteCond %{REQUEST_FILENAME} -f 
    RewriteRule ^(.*?)\.php$ /$1.html [L,R=301] 

    # If a request for an html file is made, check that it's a php file, and if so, serve the php file: 
    RewriteCond %{REQUEST_URI} ^/(.*?)\.html$ 
    RewriteCond %{DOCUMENT_ROOT}/%1.php -f 
    RewriteRule^/%1.php [L] 

    # Everything else goes to the controller 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_URI} !=/favicon.ico 
    RewriteRule ^(.*)$ mycontroller.php [L,QSA] 
</IfModule> 
+0

感謝您的回答!我完全複製並粘貼了這個,並且仍然在「沒有指定輸入文件」。除了.php文件之外的所有內容,我必須輸入.php擴展名。 – seaofinformation

+0

@seaofinformation這是一個Apache引擎問題,不確定發生這種情況的具體原因,但試着在「RewriteEngine On」指令後添加一個「RewriteBase /」。 –

+0

我也意識到我錯誤地上傳了.htaccess(我的FTP客戶端將它弄亂了)。現在它已正確上傳,結果很有意思 - 對於存在的頁面或將其導向控制器時,我得到「您請求的頁面不可用,發生了一般錯誤」。如果我爲.php文件添加一個.html擴展名,它就會進入我的404頁面。即使添加RewriteBase /行,這一切仍然會發生。 – seaofinformation

相關問題