MR_AMDEV
MR_AMDEV

Reputation: 1922

.htaccess redirect based on conditions and parameters

I have tried many answers but none of them seems to work.

WHAT I WANT?

1). I want to redirect this(if it contains the parameters from & to and both of them are not empty):

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

to

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

As you can see, we combined the values of from & to

2). And this(if it contains the parameter x along with from & to):

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

to

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

As you can see the 13 is the same as the above url.

3). And if x is empty then:

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

to

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

As you can see we set the x value default to 1

WHAT I HAVE TRIED?

    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]

I really appreciate your help, Spent a lot of time

Upvotes: 2

Views: 67

Answers (1)

anubhava
anubhava

Reputation: 784898

Assuming from parameter is always 3 characters long. You may try these rules in site root .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]

Upvotes: 2

Related Questions