简体   繁体   中英

.htaccess redirect syntax for URLs with query parameters in origin and target URLs

I have an IIS redirect rule that works perfectly. I've been trying to convert it into .htaccess format to use on the live web host, with little success.

The intent of the rule is to redirect any access of the url

http://my.web.host/user/username/?profiletab=main

to the url

http://my.web.host/network/?user=username

The IIS web.config format of the rule is as follows:

<rule name="bounce" patternSyntax="ECMAScript" stopProcessing="true">
    <match url="^user/(.*)/$" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
       <add input="{QUERY_STRING}" pattern="profiletab=main" />
       <add input="{QUERY_STRING}" pattern="um_action" negate="true" />
    </conditions>
    <action type="Redirect" url="/network/?user={R:1}" appendQueryString="false" redirectType="Found" />
</rule>

What I've tried thus far has been variations on the rule

Redirect 301 /user/(.*)/?profiletab=main /network/?user=$1

and also

RewriteCond %{THE_REQUEST} ^user/(.*)/\?profiletab=main$ [NC]
RewriteRule network/?user=%1 [R=302,L]

neither of which have worked. So I would really appreciate some assistance with the syntax needed to achieve the redirect above. (I hope I am at least close!)

Thanks in advance, folks!

You can use this rule as your very first rule in .htaccess:

RewriteEngibe On

RewriteCond %{QUERY_STRING} (?:^|&)profiletab= [NC]
RewriteRule ^user/([\w-]+)/?$ /network/?user=$1 [QSA,R=301,L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM