Tifon
Tifon

Reputation: 53

Simple htaccess rewrite rule error 404

I'm trying to set a simple htaccess rule, but is not working.
I think the problem is with the ? and = characters?

The code is:

Options +FollowSymLinks 
ErrorDocument 404 /php/404redirect.php
RewriteEngine on
RewriteRule ^productos.php?id=([0-9]+)$ /?view=productos&id=$1 [L,NE,B,QSA]

This always give me error 404.

What I want is redirect all requests from:
www.example.com/productos.php?id=X
to
www.example.com/?view=productos&id=X

Upvotes: 5

Views: 102

Answers (2)

Dusan Bajic
Dusan Bajic

Reputation: 10879

RewriteRule ^productos.php /?view=productos [L,NE,B,QSA]

or if you want to redirect

RewriteRule ^productos.php /?view=productos [L,NE,B,QSA,R=301]

Upvotes: 2

Amit Verma
Amit Verma

Reputation: 41219

Querystring is not part of match in RewriteRule directive, to redirect query strings, you need to use RewriteCond one of the following options :

option 1

RewriteEngine on

RewriteCond %{THE_REQUEST} /product\.php\?id=([^&\s] [NC]
RewriteRule ^ /?view-product&id=%1? [NC,L,R]

option 2

RewriteEngine on

RewriteCond %{QUERY_STRING} ^id=([^&]+)$ [NC]
RewriteRule ^product\.php$ /?view-product&id=%1? [NC,L,R]

We use an empty question mark ? at the end of the target url to discard the old query strings, otherwise these query strings get appened to the target url by default.

Change the R to R=301 if you want to make the redirection permanent.

Upvotes: 3

Related Questions