2

I have the following .htaccess file:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule api/(.*)$ api.php?m=$1 [QSA,NC,L]
</IfModule>

The URL rewriting works great. I can go to http://myserver.com/api/example and it will behave as if I went to http://myserver.com/api.php?m=example. The problem is that the PHP $_REQUEST and $_GET variables are empty. Shouldn't I still be able to get the value of $_REQUEST['m']?

After some googling, I found a suggestion to disable MultiViews. If I add Options -MultiViews, I get a 404 error.

What am I doing wrong? Thank you.

lampej
  • 258
  • 2
  • 9

1 Answers1

2

Sounds like you don't have mod_rewrite enabled. Since it's not enabled, the IfModule container is ignored. See: How to enable mod_rewrite for Apache 2.2

You must turn off Multiviews in order for this to work, otherwise mod_negotiation is activated and will automatically map /api/ to /api.php without giving mod_rewrite a chance to do anything. So you need the line:

Options -MultiViews

If you can't enable mod_rewrite, you could alternatively change your api.php script so that it looks in the PATH_INFO variable:

$_SERVER['PATH_INFO']

to get the "example" part.

Community
  • 1
  • 1
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • mod_rewrite is enabled. I checked phpinfo() and used the function in_array('mod_rewrite',apache_get_modules()). It is a shared server that I do not have administrative access to, if that matters. Additionally, the rewrite is actually working - it's sending /api/example to /api.php?m=example. – lampej Jan 01 '15 at 22:46
  • Actually I take that back, MultiViews is making /api/whatever redirect to api.php...and then I'm losing the $_GET. I moved the Options -MultiViews outside the IfModule and that worked. I still do not understand why I got a 404 error when it was inside IfModule. – lampej Jan 01 '15 at 22:51
  • I accepted this as the answer because MultiViews was a part of the problem. I also had to provide the full path to api.php. – lampej Jan 24 '15 at 19:21