Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 302
Tifon Avatar asked May 18 '16 10:05

Tifon


2 Answers

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.

like image 59
Amit Verma Avatar answered Sep 24 '22 19:09

Amit Verma


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]

like image 24
Dusan Bajic Avatar answered Sep 24 '22 19:09

Dusan Bajic