【问题标题】:.htaccess redirect based on conditions and parameters.htaccess 基于条件和参数的重定向
【发布时间】:2021-02-09 05:22:21
【问题描述】:

我尝试了很多答案,但似乎都没有。

我想要什么?

1). 我想重定向这个(如果它包含参数 fromto 并且它们都不是空):

https://arc.abc.com/mconvertar.php?from=gbp&to=jpy

https://arc.abc.com/gbpjpy/

如你所见,我们结合了 from & to 的值

2). 还有 this(如果它包含参数 x 以及 fromto >):

https://arc.abc.com/mconvertar.php?from=gbp&to=jpy&x=13

https://arc.abc.com/gbpjpy/x=13

如您所见,13 与上面的 url 相同。

3). 如果 x 为空,则:

https://arc.abc.com/mconvertar.php?from=gbp&to=jpy&x=

https://arc.abc.com/gbpjpy/x=1

如您所见,我们将 x 值默认设置为 1

我尝试了什么?

    RewriteCond %{QUERY_STRING} (^|&)from=[^&\s]&to=[^&\s]&x=[0-9]
    RewriteRule ^(.*)$ https://arc.abc.com/gbpjpy/x=%3 [R=301,L]

    RewriteCond %{QUERY_STRING} !(^|&)x=($|&)
    RewriteRule ^(.*)$ https://arc.abc.com/gbpjpy/x=1 [R=301,L]

    RewriteRule ^([^/]*)([^/]*)/([^/]*)\.html$ /mconvertar.php?from=$1&to=$2 [L]

非常感谢您的帮助,花了很多时间

【问题讨论】:

    标签: regex apache .htaccess redirect


    【解决方案1】:

    假设from 参数总是3 个字符长。您可以在站点根目录 .htaccess 中尝试这些规则:

    RewriteEngine On
    
    # external redirect from actual URL to pretty one with x=<digit>
    RewriteCond %{THE_REQUEST} /mconvertar\.php?from=(\w+)&to=([^\s&]+)&(x=\d+)\s [NC]
    RewriteRule ^ /%1%2/%3? [R=302,L]
    
    # external redirect from actual URL to pretty one with x=<empty>
    RewriteCond %{THE_REQUEST} /mconvertar\.php?from=(\w+)&to=([^\s&]+)&(x)=\s [NC]
    RewriteRule ^ /%1%2/%3=1? [R=302,L]
    
    # internal forward from pretty URL to actual one
    RewriteRule ^(\w{3})([^/]+)/(x=\d+)/?$ mconvertar.php?from=$1&to=$2&$3 [L,QSA,NC]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-30
      • 2018-01-22
      • 2015-04-12
      • 1970-01-01
      • 1970-01-01
      • 2012-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多