簡體   English   中英

.htaccess RewriteRule api / v1 /(.*)$ api / v1 / api.php?request = $ 1 [QSA,NC,L]不起作用

[英].htaccess RewriteRule api/v1/(.*)$ api/v1/api.php?request=$1 [QSA,NC,L] not working

我想將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>

我在Mac Yosemite上使用服務器:Apache / 2.4.9(Unix)PHP / 5.5.14。 我已經取消注釋LoadModule的mod_rewrite並將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>

任何幫助,將不勝感激!

我能夠解決我的問題。

  1. 首先,我使用此站點測試了我的重寫規則: http : //htaccess.madewithlove.be
  2. 從那里我找出了正確的規則應該是:

     RewriteEngine On RewriteRule api/v1/(.*)$ /api/v1/api.php?request=$1 [QSA,NC,L] 
  3. 我還注意到,僅當我將這些規則直接放在httpd.conf文件而不是單獨的.htaccess文件中時,才需要<IfModule mod_rewrite.c></IfModule> 因此,我刪除了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

您不需要所有.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]

嘗試使用此代碼。

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]   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM