Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess redirect not working with URLs with parameters

I want to redirect index.php?action=this&id=1 to index.php?action=this&id=2

I tried the code below in my .htaccess but it didn't help

redirect 301 index.php?action=this&id=1 http://mysite.com/index.php?action=this&id=2

What am i doing wrong here? what could be a workaround?

like image 699
rzr Avatar asked Dec 02 '25 19:12

rzr


2 Answers

In order to match specific query string you have to use mod_rewrite. Please check if it is installed/allowed on your host. The rule in this case will be something like this:

# most likely be required for rewrite rules to function properly
Options +FollowSymLinks +SymLinksIfOwnerMatch

# Activate Rewrite Engine
RewriteEngine On
RewriteBase /

# actual rule
RewriteCond %{QUERY_STRING} ^action=this&id=1 [NC]
RewriteRule ^index\.php$ /index.php?action=this&id=2 [R=301,L]

This needs to be placed in .htaccess in website root folder. If placed anywhere else some small changes may be required.

This rule will only redirect /index.php?action=this&id=1 to /index.php?action=this&id=2 and no other URLs (just as you asked in your question).

like image 52
LazyOne Avatar answered Dec 05 '25 16:12

LazyOne


You could try just adding this on the page that you want redirected

<meta HTTP-EQUIV="REFRESH" content="0; url=index?action=this&id=2">
like image 37
kyle1046 Avatar answered Dec 05 '25 16:12

kyle1046



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!