4

I'm currently redirecting all requests to the index.php file using the following:

<IfModule mod_rewrite.c>
RewriteEngine on

# base path, if not the root directory
RewriteBase /dev/public/

# direct all requests to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php
</IfModule>

While this works, it limits my code to only being able to run on servers with mod_rewrite enabled. So I'm wondering, is there a way to simulate this behavior without using .htaccess? How do other frameworks do it?

mister martin
  • 6,197
  • 4
  • 30
  • 63

3 Answers3

1

you can write a short php script:

<?php
    header( 'Location: index.php' ) ;
?>

and include it on the top of all your php files except index.php

Dima
  • 8,586
  • 4
  • 28
  • 57
  • I believe that would only work for `php` files and not 404, or asset requests. – Dave Chen Feb 15 '14 at 18:33
  • 2
    This only works for URLs that already point at a PHP script, and tosses out the URL info, post data, etc. That makes it kinda worthless. – cHao Feb 15 '14 at 18:33
1

if you dont want to user htaccess you can make your all link like index.php?c=products&m=view&id=345 so your all request is going to the index.php any way and you dont need htaccess

Minhaz
  • 446
  • 5
  • 7
  • I don't see anywhere in the question mentioning CodeIgniter. Any reason you assumed that was what was being used? – IMSoP Feb 15 '14 at 18:56
  • I was reading the CodeIgniter tags and i am not sure how i did came to this link, and when i saw the htaccess it was looking like the general CI htaccess. If the framework is not CI the answer is not relevant at all. Sorry for that. – Minhaz Feb 15 '14 at 19:04
  • But it dose not it doesn't matter its CI or not if you dont want to user htaccess you have to user index.php?YOUR_GET in your link – Minhaz Feb 15 '14 at 19:07
1

Hosts like Yahoo don't allow .htaccess, which can be a little frustrating. Redirecting like that isn't possible as far as I know, but this is a workaround:

have this file structure:

index.php
private/ <-- you could do: password protect, set permissions, name with hard to guess string, etc
public/

So accessing example.com/page will output 404, but you can redo your urls to example.com/index.php/page This is what I use to get the URI args in an array:

$uri_args = 
       explode('/', substr($_SERVER['REQUEST_URI'], strpos($_SERVER['REQUEST_URI'], 'index.php') + 10));

Optionally, to delete the empty args: array_filter($uri_args);

See my question: How To Rewrite URL in PHP Only?
(See also http://php.net/manual/en/reserved.variables.server.php and Remove empty array elements)

Community
  • 1
  • 1
  • That code seems rather obfuscated - is that `?:` being used as some kind of weird `if` statement? – IMSoP Feb 15 '14 at 18:59
  • @IMSoP I wouldn't say weird. It is extremely helpful for returning expressions conditionally, whereas `if` statements can't return (they can't be used as an expression) - see http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary –  Aug 04 '14 at 20:30
  • Sure, but you weren't using it to return an expression conditionally, you were using it to set a variable conditionally. Or at least I think you were, I'd have to look up the operator precedence to even figure out what ends up where. I'd probably split it into at least two lines, with an intermediate variable, like this: http://pastebin.com/YhsCcwzc And even without that, a comment is kind of handy on something as nested as that. – IMSoP Aug 04 '14 at 21:28
  • The part after the '=' is an expression, and expressions, im pretty sure, "return" things –  Aug 04 '14 at 21:37
  • I think you're just splitting hairs now. The distinction is between executing an expression for its value, and executing it for its side-effect. Unless you're trying to win at code golf, there's little reason to use the return value of an assignment (beyond simple cases like initialising multiple counters: `$x = $y = 0;`), and good reasons not to (readability and maintainability). – IMSoP Aug 04 '14 at 22:04
  • 1
    All that being said, [the version of the code we're discussing](http://stackoverflow.com/revisions/21802039/1) seems to have been a copying mistake or similar anyway, and you quickly removed the extra expression, so for all I know it may have been copied from code that wasn't as hard to follow. – IMSoP Aug 04 '14 at 22:07