2012-09-13 47 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

相关问题