开发者

RewriteCond on .htaccess

I'm writing a RESTful API for my web service.

I want all the calls of the following pattern trigger a php file that is that is located in /myserver/api/controller.php The pattern is :

http:/www.mydomain.com/api/user

http:/www.mydomain.com/api/resource

Basically, all the calls for http://www.mydomain.com/api/* should trigger /api/controller.php

Currently, my .htaccess is in /api and looks like this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . controller.php
开发者_如何转开发

UPDATE 1:

The .htaccess in the parent directory is:

RewriteEngine On
RewriteCond %{HTTP_HOST} !:9000$
RewriteCond %{SCRIPT_FILENAME} \.php$ [OR]
RewriteCond %{REQUEST_URI} ^$
RewriteRule ^(.*)$ http://%{HTTP_HOST}:9000/$1 [P,L]
RedirectMatch 301 /jobs$ http://someurl
RedirectMatch 301 /support/$ http://someurl
RedirectMatch 301 /jobs$ http://someurl
RedirectMatch 301 /someurlA http://someurlB

How should I write the .htaccess file?

Thanks!


How about this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ controller.php?q=$1 [L,QSA]

This would redirect a request like /api/user/5 to /api/controller.php?q=user/5. The ''QSA'' will also preserve any query string variables that are already present.


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.* controller.php [L]

Update:

RewriteEngine On
RewriteBase /api
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.* controller.php [L]


It's not clear that Apache is even reading your .htaccess file at all. Try putting the Wooga non-directive in there and see if you get a 500 error.

Also, don't use .htaccess files for mod_rewrite (or anything else, for that matter) unless you have no other choice.

UPDATE 1

Ok, so your .htaccess file is being read. The next step is to try a simpler rule. Something like:

RewriteEngine On
RewriteRule ^ http://httpd.apache.org/

If that works, then you know mod_rewrite is operating and the problem is with the matching in your rules.

UPDATE 2

So mod_rewrite is working, meaning that your rules are not matching. Try removing the conditions but leaving the rule as it is.

RewriteEngine On
RewriteRule . controller.php

If that causes a loop, change it to this:

RewriteEngine On
RewriteRule !controller.php controller.php
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜