【问题标题】:.htaccess convert url from route parameter to query string with blank parameter value.htaccess 将 url 从路由参数转换为具有空白参数值的查询字符串
【发布时间】:2020-10-23 08:37:49
【问题描述】:

我有 .htaccess 问题,其中 url 在路由参数中,但我想将其转换为查询字符串格式。

当前路径是:
http://localhost:4200/act/sso/app/2040/token/7d2f-4ddd-924f-3fd36572/address/chd/city/chd/state/chd

我想将其转换为:http://localhost:4200/act/sso?app=2040&token=7d2f-4ddd-924f-3fd36572&address=chd&city=chd&state=chd

我在下面写了 .htaccess 规则。

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^act/sso/app/32/token/([^/]*)/address/([^/]*)/city/([^/]*)/state/([^/]*)

RewriteRule ^act/sso/app/32/token/([^/]*)/address/([^/]*)/city/([^/]*)/state/([^/]*) act/sso?appID=32&mfaToken=$1&address=$2&city=$3&state=$4 [R=301,L]

我的问题是当我们获取可选参数值时,我的路由规则不匹配。

见下面的例子。

http://localhost:4200/act/sso/app/2040/token/7d2f-4ddd-924f-3fd36572/address//city//state/chd

http://localhost:4200/act/sso/app/2040/token/7d2f-4ddd-924f-3fd36572/address/chd/city//state/chd

http://localhost:4200/act/sso/app/2040/token/7d2f-4ddd-924f-3fd36572/address/chd/city/chd/state/

请帮忙。

【问题讨论】:

    标签: angular apache .htaccess


    【解决方案1】:

    您能否尝试使用所示示例进行以下、编写和测试。

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} ^/act/sso/app/([0-9]+)/token/([^/]*)/address/([^/]*)/city/([^/]*)/state/([^/]*)$
    RewriteRule ^.*$ /act/sso?app=%1&token=%2&address=%3&city=%4=state=%5 [L,NE,NC,QSA]
    

    【讨论】:

    • 仍然没有维护正确的 url。对于空白值,apache 将双斜杠转换为单斜杠,如 act/sso/app/20/token/7d2f572/address/city/state/chd。我们可以为空值设置空值吗?那么它将是正确的,我们可以更换。比如/act/sso/app/20/token/7d2f572/address/null/city/null/state/chd
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-11
    • 2021-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多