2015-08-15 50 views
0

我想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.10(Unix的)PHP/5.5.14在Mac上的優勝美地。我註釋掉了的LoadModule mod_rewrite的,並設置了AllowOverride所有:

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> 

任何幫助,將不勝感激!

+0

@anubhava/Library/WebServer/Do作爲我的PHP文件的地方 相同的地方。 –

+0

是的,給我500錯誤,所以它正在使用@anubhava –

+0

你必須使用Apache 2.4,而不是2.2。 「要求所有授予的」在2.2中不存在,並且如果使用將會引發錯誤。請確認您使用的是哪個版本。 –

回答

0

在你的.htaccess文件中並不需要全部[QSA,NC,L]

簡單的嘗試

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] 
+0

試過了,仍然一樣。不起作用。我正在關注此:http://coreymaynard.com/blog/creating-a-restful-api-with-php/ –

+0

仍然相同,不起作用:( –

+0

我能解決我的問題@哈桑 –

1

我能解決我的問題。

  1. 首先,我用這個網站來測試我的重寫規則:http://htaccess.madewithlove.be
  2. 從那裏我想通了,我正確的規則應該是:

    RewriteEngine On 
    
    RewriteRule api/v1/(.*)$ /api/v1/api.php?request=$1 [QSA,NC,L] 
    
  3. 我也注意到,<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> 
    
  4. 重新啓動Apache

  5. 測試使用curl:

    curl http://localhost/api/v1/example 
    
  6. 應該重定向到http://localhost/api/v1/api.php?request=example