PHP重定向在mod_rewrite(.htaccess)文件后停止工作

时间:2014-08-19 15:47:14

标签: php apache .htaccess mod-rewrite redirect

自从我将以下代码

以来,我的重定向逻辑就停止了工作
Options -Indexes
ErrorDocument 404 /site3/public/admin/filenotfound.php

RewriteEngine On
RewriteBase /site3/public/admin/

RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]

RewriteCond %{THE_REQUEST} \s([^.]+?)(?:\.php)?\?caseid=([^&\s]+)?\&picid=([^&\s]+)\s [NC]
RewriteRule ^ %1/caseid/%2/picid/%3/? [R=302,L,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/caseid/([^/]+)/picid/([^/]+)/?$ $1.php?caseid=$2&picid=$3 [L,NC,QSA]

RewriteCond %{THE_REQUEST} \s([^.]+?)(?:\.php)?\?caseid=([^&\s]+)\s [NC]
RewriteRule ^ %1/caseid/%2/? [R=302,L,NE]

RewriteCond %{THE_REQUEST} \s([^.]+?)(?:\.php)?\?search=([^&\s]+)\s [NC]
RewriteRule ^ %1/search/%2/? [R=302,L,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/caseid/([^/]+)/?$ $1.php?caseid=$2 [L,NC,QSA]

## hide .php extension snippet    
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} \s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=302,L,NE]

# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

这就是我重定向页面的方式

function redirectTO($url = null){
    if($url != null){
        header("Location:{$url}");
        exit();
    }
}

redirectTO('page.php');

我不是一个非常好的模式重写,因此我无法解决问题所在,请告诉我应该怎样,如果删除我的.htaccess文件不能正常工作如果把那个文件重新开始工作。

任何想法?

2 个答案:

答案 0 :(得分:1)

使用您网址的完整路径,即www.example.com/page.php

答案 1 :(得分:1)

由于您使用重写规则删除.php扩展并添加尾部斜杠,请使用此PHP代码重定向:

function redirectTO($url = null){
    if($url != null){
        header("Location: /site3/public/admin/" . $url);
        exit();
    }
}

redirectTO('page/');