【问题标题】:Mod Rewrite - Clean URL with query string not workingMod Rewrite - 带有查询字符串的清理 URL 不起作用
【发布时间】:2019-09-03 11:05:38
【问题描述】:

我有以下 .htaccess 文件。

<IfModule mod_rewrite.c> 
    Options +FollowSymlinks
    RewriteEngine On
    DirectoryIndex api.php
    FallbackResource index.php

    RewriteCond %{REQUEST_URI} ^/api
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{QUERY_STRING} ^$
    RewriteRule ^/([^/]+)/([^/]+)$ $1/$1.php?endpoint=$2 [L]
    RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ $1/$1.php?endpoint=$2&id=$3 [L]
    RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)$ $1/$1.php?endpoint=$2&id=$3&endpoint2=$4 [L]
    RewriteCond %{QUERY_STRING} ^(.*)$
    RewriteRule ^([^/]+)$ $1/$1.php  [L]
    RewriteRule ^([^/]+)/([^/]+)? $1/$1.php?endpoint=$2%1 [QSA,L]
    RewriteRule ^/([^/]+)/([^/]+)/([^/]+)? $1/$1.php?endpoint=$2&id=$3%1 [QSA,L]
    RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)? $1/$1.php?endpoint=$2&id=$3&endpoint2=$4%1 [QSA,L]
</IfModule> 

我想将 api 端点(干净的 url 格式,可能在最后一个令牌末尾查询)完全重写为查询字符串格式,如下所示。

例子

api/users/123/actionitems

当前读取

api/api.php?endpoint=users&id=123&endpoint2=actionitems

{
    endpoint: users,
    id: 123,
    endpoint2: actionitems
}  

但我也想转换

api/users/123/actionitems?test=3

进入

api/api.php?endpoint=users&id=123&endpoint2=actionitems&test=3

{
    endpoint: users,
    id: 123,
    endpoint2: actionitems,
    test: 3
}  

它不起作用。我只得到

api/api.php?endpoint=users&id=123&endpoint2=actionitems

当我输入时

/api/users/123/actionitems?test=3

{
    endpoint: users,
    id: 123,
    endpoint2: actionitems
}  

只有当我在请求中输入 /api/users/123/actionitems&amp;test=3 而不是问号 (/api/users/123/actionitems?test=3) 时,它才有效。

如何让它发挥作用?

【问题讨论】:

  • 尝试删除倒数第二条规则模式中的前导斜杠。 RewriteRule ^ **/** ([^/]+)/([^/]+)/([^/]+)? .
  • 不幸的是还是一样的结果。

标签: apache .htaccess mod-rewrite url-rewriting clean-urls


【解决方案1】:

您可以使用以下单一规则:

    Options +FollowSymlinks
    RewriteEngine On
    DirectoryIndex api.php
    FallbackResource index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^api/([^/]+)/([^/]+)/([^/]+)/?$  /api/api.php?endpoint=$1&id=$2&endpoint2=$3 [QSA,L]

【讨论】:

  • 有效!但我仍然需要进行调整以允许问题中的 1 或 2 令牌案例
  • @Vahe 这应该适用于两个网址 api/users/123/actionitems?test=3api/users/123/actionitems
  • 我修改了规则并发布了第二个答案,但我会奖励你答案。
【解决方案2】:

工作规则(到目前为止...)

<IfModule mod_rewrite.c> 
    Options +FollowSymlinks
    RewriteEngine On
    DirectoryIndex api.php
    FallbackResource index.php

    RewriteCond %{REQUEST_URI} ^/api
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f

    RewriteCond %{QUERY_STRING} ^(.*)$
    RewriteRule ^api/$ /api/api.php? [L]
    RewriteRule ^api/([^/]+)/$ /api/api.php?endpoint=$1%1 [QSA,L]
    RewriteRule ^api/([^/]+)/([^/]+)$ /api/api.php?endpoint=$1&id=$2%1 [QSA,L]
    RewriteRule ^api/([^/]+)/([^/]+)/([^/]+)$  /api/api.php?endpoint=$1&id=$2&endpoint2=$3%1 [QSA,L]
    #Disgard tokens after 3rd token
    RewriteRule ^api/([^/]+)/([^/]+)/([^/]+)/(.*)$  /api/api.php?endpoint=$1&id=$2&endpoint2=$3%1 [QSA,L]
</IfModule>   

【讨论】:

    猜你喜欢
    • 2011-02-08
    • 1970-01-01
    • 2012-07-21
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多