简体   繁体   English

.htaccess上的RewriteCond

[英]RewriteCond on .htaccess

I'm writing a RESTful API for my web service. 我正在为我的Web服务编写RESTful API。
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 : 我希望以下模式的所有调用触发位于/myserver/api/controller.php中的php文件,该模式为:
http:/www.mydomain.com/api/user HTTP:/www.mydomain.com/api/user
http:/www.mydomain.com/api/resource HTTP:/www.mydomain.com/api/resource

Basically, all the calls for http://www.mydomain.com/api/ * should trigger /api/controller.php 基本上,对http://www.mydomain.com/api/ *的所有调用都应触发/api/controller.php

Currently, my .htaccess is in /api and looks like this: 目前,我的.htaccess位于/ api中,如下所示:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . controller.php

UPDATE 1: 更新1:
The .htaccess in the parent directory is: 父目录中的.htaccess是:

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? 我应该如何编写.htaccess文件?

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 . 这样会将/api/user/5类的请求重定向到/api/controller.php?q=user/5 The ''QSA'' will also preserve any query string variables that are already present. “ QSA”还将保留所有已经存在的查询字符串变量。

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. 尚不清楚Apache甚至根本没有读取您的.htaccess文件。 Try putting the Wooga non-directive in there and see if you get a 500 error. 尝试将Wooga非指令放入其中,看看是否出现500错误。

Also, don't use .htaccess files for mod_rewrite (or anything else, for that matter) unless you have no other choice. 另外,除非您别无选择,否则请勿将.htaccess文件用于mod_rewrite(或其他任何方式)。

UPDATE 1 更新1

Ok, so your .htaccess file is being read. 好的,因此正在读取您的.htaccess文件。 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. 如果可行,则说明mod_rewrite正在运行,而问题出在规则中的匹配上。

UPDATE 2 更新2

So mod_rewrite is working, meaning that your rules are not matching. 因此mod_rewrite可以正常工作,这意味着您的规则不匹配。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM