views:

64

answers:

3

I'm trying to catch all .php requests so that my visitors won't be able to see the php magic

I want example.com/home to be redirected to example.com/index.php, but I don't want direct access to example.com/index.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Although it seems to work at first glance, it doesn't prevent users from visiting example.com/index.php directly.

How would I solve this?


In reply to an answer:

RewriteRule ^index.php$ / [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

RewriteRule ^home/?$ /index.php?category=1 [L]

example.com/home outputs example.com/category=1

A: 

Check this link

How to prevent infinite redirect loop on htaccess ?

JapanPro
+1  A: 

You could run a preg_match() for .php on the $_SERVER['REQUEST_URI'] variable, then use a header() to redirect accordingly. For example, in index.php:

if(preg_match('/\.php/', $_SERVER['REQUEST_URI'])) {
    header('Location: home');
    die();
}

That has no support for any query string parameters, however, as I don't know what your format is in your /home rewrite. $_SERVER['QUERY_STRING'] will be your friend here, though.

chigley
+1  A: 

Try this:

RewriteRule ^index.php$ / [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

What that will do is, if the user tries to go directly to index.php, it will redirect them to the root of the site. Otherwise, it will run your existing rules, passing anything through to index.PHP.

Josh
The problem is that redirecting test/(.*)/?$ to index.php?q=$1 results in example.com/?q=variable
Kriem
It shouldn't, if the index.php rewrite rule is first... I'll test this for you tomorrow and confirm.
Josh
Thanks! Looking forward to the test result. :)
Kriem