0

Iam trying to do 301 redirection. But it doesnt get redirected.

My old URL : https://www.example.com/category.php?filter=Short%20Term%20Renting

My new URL : https://www.example.com/services/short-term-renting

My code in .htaccess:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^([^&]&)*filter=Short%20Term%20Renting(&|$)
RewriteRule ^category.php/services$ /short-term-renting? [L,R=301]

I referred with this link "301 Redirecting URLs based on GET variables in .htaccess"

newbie
  • 115
  • 1
  • 1
  • 14

1 Answers1

1

You are matching ^category.php/services whereas services is not present in your current request URI. Also, I would suggest matching query string regardless of the case of characters.So, your .htaccess should look something like this:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^.*?filter=Short%20Term%20Renting$ [NC]
RewriteRule ^category.php$ /services/short-term-renting? [NC,L,R=301]

Demo: https://htaccess.madewithlove.be?share=8acfa0a0-cf9e-5036-a245-a5a24bd90b26

Update:

If you want to serve the services/* URL, yet make an internal request to the same category.php, you could do something like below:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^.*?filter=Short%20Term%20Renting$ [NC]
RewriteRule ^category.php$ /services/short-term-renting? [L,R=301]
RewriteRule ^services\/(.*)$ /category.php?filter=$1 [L]

Demo: https://htaccess.madewithlove.be?share=8f82a119-5cc9-5851-a3fa-ad9171ef6487

nice_dev
  • 17,053
  • 2
  • 21
  • 35
  • Ok its working fine. But I need 301 redirection like here. So that when I redirect only the name of the URL must change. The content in the web page must be same. – newbie Oct 17 '19 at 10:36