Redirect rule for multiple conditions

Alles, was den Apache betrifft, kann hier besprochen werden.

Redirect rule for multiple conditions

Postby sdbhabal » 27. September 2019 11:44

Hello Experts,

I need conditional redirection rule like below:

Code: Select all
RewriteRule l/?([-a-zA-Z0-9_+]+)$ short/new1_rdr.php?d=$1 [L]
RewriteRule s/?([-a-zA-Z0-9_+]+)$ short/new2_rdr.php?d=$1 [L]
RewriteRule ^/?([-a-zA-Z0-9_+]+)$ short/new.php?d=$1 [L]


In above rules, what I wanted to achieve is:
1. whenever http://abc.com/l/gdff gets request it should get redirected to short/new1_rdr.php file.
2. whenever http://abc.com/s/hdvb gets request it should get redirected to short/new2_rdr.php file.
3. And if anything other than 'l or s' in the request like http://abc.com/k/hdvb, it should get redirect to short/new.php file.

Above redirection rules are not working as expected.
Please help me to solve this.
sdbhabal
 
Posts: 4
Joined: 05. July 2018 13:48
XAMPP version: 7.2.6
Operating System: Centos

Re: Redirect rule for multiple conditions

Postby Nobbie » 27. September 2019 13:13

I cannot see anything wrong with rule 1 and 2, only the third is incorrect:

RewriteRule ^/?([-a-zA-Z0-9_+]+)$ short/new.php?d=$1 [L]


Instead:

Code: Select all
RewriteRule [^ls]/?([-a-zA-Z0-9_+]+)$ short/new.php?d=$1 [L]


P.S.: Actually, i dont think that "/?" is correct, simply without "?":

Code: Select all
RewriteRule l/([-a-zA-Z0-9_+]+)$ short/new1_rdr.php?d=$1 [L]
RewriteRule s/([-a-zA-Z0-9_+]+)$ short/new2_rdr.php?d=$1 [L]
RewriteRule [^ls]/([-a-zA-Z0-9_+]+)$ short/new.php?d=$1 [L]


Last not least, why so complicated pattern for the part behind the slash, why not simply ".*"?

Code: Select all
RewriteRule l/(.*)$ short/new1_rdr.php?d=$1 [L]
RewriteRule s/(.*)$ short/new2_rdr.php?d=$1 [L]
RewriteRule [^ls]/(.*)$ short/new.php?d=$1 [L]
Nobbie
 
Posts: 11157
Joined: 09. March 2008 13:04

Re: Redirect rule for multiple conditions

Postby sdbhabal » 30. September 2019 05:36

Thank you Nobbie for your quick response, detailed explanation & valuable suggestion.
I have done the required changes as per your suggestion.
But I don't know why, now everything is getting redirect to short/new.php?d=$1 (to the third rule).
Even 'l' & 's' also, getting redirect to short/new.php?d=$1.
If i remove third rule, then first & second is working fine.
Please help.
sdbhabal
 
Posts: 4
Joined: 05. July 2018 13:48
XAMPP version: 7.2.6
Operating System: Centos

Re: Redirect rule for multiple conditions

Postby Nobbie » 30. September 2019 12:14

You have to add "^" in the beginning of the pattern (= first character). This .htaccess works for me:

Code: Select all
RewriteEngine On
RewriteRule ^l/(.*)$ short/new1_rdr.php?d=$1 [L]
RewriteRule ^s/(.*)$ short/new2_rdr.php?d=$1 [L]
RewriteRule ^[^ls]/(.*)$ short/new.php?d=$1 [L]
Nobbie
 
Posts: 11157
Joined: 09. March 2008 13:04

Re: Redirect rule for multiple conditions

Postby sdbhabal » 01. October 2019 09:27

It worked for me too.
Thank you so much again.
sdbhabal
 
Posts: 4
Joined: 05. July 2018 13:48
XAMPP version: 7.2.6
Operating System: Centos


Return to Apache

Who is online

Users browsing this forum: No registered users and 6 guests