Chin Leung
Chin Leung

Reputation: 14921

Need help for .htaccess RewriteRule

I've been trying to setup a RewriteRule for my .htaccess to redirect any not found .php url to my loader.php ... For some reasons, it's redirecting even if the file exist.

Here's what I have:

# Engine On for Extension Removal
RewriteEngine On

# Rewrite the url for the login page
RewriteRule ^login/?$ index.php?tab=login [NC,L]

# Load the url with the page loader if the url doesn't match any of the rule above
RewriteRule ^(.*)\.php$ loader.php?page=$1 [NC,L]

# Remove PHP Extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Everything is working before I added this line:

RewriteRule ^(.*)\.php$ loader.php?page=$1 [NC,L]

Right now, no matter what is in my link, even if it exist or not, it'll load the loader.php. And I have other rules like the login one, same thing but different name which is why I didn't bother posting them.

Upvotes: 0

Views: 36

Answers (1)

Amit Verma
Amit Verma

Reputation: 41219

Try

# Engine On for Extension Removal
RewriteEngine On

# Rewrite the url for the login page
RewriteRule ^login/?$ index.php?tab=login [NC,L]


# Remove PHP Extensio

 RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.php$ loader.php?page=$1 [NC,L]

The problem with

RewriteRule ^(.*)\.php$ loader.php?page=$1 [NC,L]

is that it accepts any .php url in the request and rewrites it to loader.php.

The condition

RewriteCond %{REQUEST_FILENAME} !-f

change this behaviour of the rule, it tells server to process the rule only when there is a request for non existent php file.

Upvotes: 1

Related Questions