Sharif
Sharif

Reputation: 728

PHP redirect stop working after mod_rewrite (.htaccess) file

My redirect logic has stopped working ever since I've put the following code

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]

this is how i'm redirecting my page

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

redirectTO('page.php');

I'm not a very good of mode rewriting therefore i cannot work out where the issue is please advise me what should, by the way they work fine if remove my .htaccess file doesn't work if put that file back on.

Any Idea?

Upvotes: 1

Views: 154

Answers (2)

anubhava
anubhava

Reputation: 785196

Since you're removing .php extension using rewrite rules and adding a trailing slash use this PHP code to redirect:

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

redirectTO('page/');

Upvotes: 1

Amjad
Amjad

Reputation: 2090

use full path of your of the URL i.e. www.example.com/page.php

Upvotes: 1

Related Questions