.htaccess with two parameters redirects to a 404 page

182 Views Asked by At

I want to redirect /index.php?id=1&time=10 to /first-10.

.htaccess:

RewriteEngine On
RewriteCond %{QUERY_STRING} id=1&time=10
RewriteRule ^index\.php$ /first-10/? [L,R=301]

when I access /index.php?id=1&time=10 redirects to /first-10/, but it's a 404 page. Please notice last slash also.

Where is the problem?

2

There are 2 best solutions below

8
Justin Iurman On BEST ANSWER

You need to internally rewrite it back to its old-original format.
You must use THE_REQUEST to avoid an infinite loop

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/(?:index\.php)?\?id=1&time=10\s [NC]
RewriteRule ^ /first-10? [R=301,L]

RewriteRule ^first-10$ /index.php?id=1&time=10 [L]
  • Both http://example.com/index.php?id=1&time=10 and http://example.com/?id=1&time=10 will redirect to http://example.com/first-10.
  • http://example.com/first-10 will internally rewrite it back to /index.php?id=1&time=10
0
David On

If really the problem is .htaccess, the result page is correct and the redirection is called, I tinking in:

The "?" character betwhen first-10/ and [L,R= ??? Or the / after first-10 ???