我希望http://localhost/api/v1/example重定向到http://localhost/api/v1/api.php?request=example
我有以下。htaccess,但它不工作:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule api/v1/(.*)$ api/v1/api.php?request=$1 [QSA,NC,L]
</IfModule>
我使用的服务器:Apache/2.4.9 (Unix) PHP/5.5.14在Mac Yosemite。我取消了mod_rewrite的LoadModule的注释,并将AllowOverride设置为All:
DocumentRoot "/Library/WebServer/Documents"
<Directory "/Library/WebServer/Documents">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options FollowSymLinks Multiviews
MultiviewsMatch Any
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>
我能够解决我的问题。
- 首先,我使用这个网站来测试我的重写规则:http://htaccess.madewithlove.be
从那里我发现我的正确规则应该是:
RewriteEngine On RewriteRule api/v1/(.*)$ /api/v1/api.php?request=$1 [QSA,NC,L]
我还注意到,
<IfModule mod_rewrite.c>
和</IfModule>
只有在我将这些规则直接放在httpd.conf文件中而不是单独的.htaccess文件时才需要。因此,我删除了我的htaccess文件,并将以下内容直接放在httpd.conf文件的最底部:<IfModule mod_rewrite.c> RewriteEngine On RewriteRule api/v1/(.*)$ /api/v1/api.php?request=$1 [QSA,NC,L] </IfModule>
重新启动apache
Test using curl:
curl http://localhost/api/v1/example
应该重定向到http://localhost/api/v1/api.php?request=example
你不需要所有的[QSA,NC,L]
在你的.htaccess
文件。
简单的尝试
RewriteEngine On
RewriteRule ^api/v1/([^/]*)$ /api/v1/api.php?request=$1 [L]
RewriteEngine On
RewriteRule ^api/v1/(.*)$ /api/v1/api.php?request=$1 [QSA,NC,L]
试试下面的代码。
RewriteEngine
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ api.php?rquest=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ api.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ api.php [QSA,NC,L]