Belatar Yassine
Belatar Yassine

Reputation: 163

Mod rewrite issue redirect with post

In fact I am working on a small php script, and now I am struggling with doing redirect using mod-rewrite. What i want is to redirect

www.xx.com/motivational/api.php?latest=1 

to

www.xx.com/api.php?latest=1&app=motivational

I tried this but it doesn't work:

RewriteRule ^/motivational/api\.php$ /api.php?latest=&%{QUERY_STRING}

Upvotes: 1

Views: 34

Answers (2)

OscarJ
OscarJ

Reputation: 413

%{QUERY_STRING} represents the entire query string, in your case latest=1. So when you append it to ...?latest= in your substitution string the result is ...?latest=latest=1 which is not what you want.

Change your rule to

RewriteRule ^/motivational/api\.php$ /api.php?%{QUERY_STRING}&app=motivational

and you should be fine.

Or you could do:

RewriteRule ^/motivational/api\.php$ /api.php?app=motivational [QSA]

The QSA flag means to append the new query string to the old, rather than to replace it, so your latest variable will not be lost.

Upvotes: 2

Amit Verma
Amit Verma

Reputation: 41219

You can not match against query strings in a RewriteRule, You will need a RewriteCond to match query strings in url :

RewriteEngine on

RewriteCond %{THE_REQUEST} /([^/]+)/api\.php\?latest=1 [NC]
RewriteRule ^ /api.php?latest=1&app=%1 [NC,L,R]

%1 is part of the regex "([^/]+)" in RewriteCond, it contains dynmically captured path in the request line.

Upvotes: 1

Related Questions