Danish
Danish

Reputation: 115

htaccess redirect not working for some reason

I am trying to figure out this code why it is not working

old url

 news.php?catID=text-from-database&nid=number-from-database

new url

 news-detail.php?cid=Cat-ID-Number&nid=number-from-database

or new url

 domain.com/number-from-database

I want to redirect permanently using below code. But it is not working.

Redirect 301 ^news.php?catID=([A-Za-z0-9-]+)&nid=([0-9]+) news-detail.php?cid=$1&nid=$2
Redirect 301 ^news.php?catID=([A-Za-z0-9-]+)&nid=([0-9]+) /$1

Upvotes: 0

Views: 39

Answers (1)

Amit Verma
Amit Verma

Reputation: 41249

You can't match against the query string in a REDIRECT Directive. You need to match against the %{QUERY_STRING} or %{THE_REQUEST} variable using Mod-rewrite:

Try :

RewriteEngine on
RewriteCond %{QUERY_STRING} ^catID=([A-Za-z0-9-]+)&nid=([0-9]+)$ [NC]
RewriteRule ^ /news-detail.php?cid=%1&nid=%2 [L,R]

or :

RewriteEngine on
RewriteCond %{THE_REQUEST} /news\.php\?catID=([A-Za-z0-9-]+)&nid=([0-9]+) [NC]
RewriteRule ^ /news-detail.php?cid=%2 [L,R]

Upvotes: 1

Related Questions