2

I am struggling to get the rewrite rules working in .htaccess for search links on a property rental website I am working on.

The old links are like this:

example.com/index.php?proptype=Villa&action=search

and the new search link equivalent will be like this:

example.com/listing?holidaytype=Villa

Before I tried with wildcards I wanted to test with a set parameter. I have tried both of the following approaches but neither works.

RewriteRule index.php?proptype=Villa&action=search listing?holidaytype=Villa [L]
RewriteCond %{QUERY_STRING} ^proptype=Villa&action=search$
RewriteRule ^index.php listing?holidaytype=Villa [L]

The only lines above these in my .htaccess file are as follows:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

Having checked a number of other related questions here and on other rewrite guidelines, I still can't get the rules working. What have I missed - or could it be another problem?

Graham

  • What happens exactly? /listing?holidaytype=Villa - A URL like this is unlikely to map directly to the filesystem so there must be some other processing going on here? Does this map to listing.php perhaps? – MrWhite Nov 21 '19 at 11:26
  • I am not the main developer on this rebuild and am just looking at the redirects from the old/existing site, but yes that link /listing?...does seem to open listing.php - or possibly another file with handles the parameters first. – Graham White Nov 21 '19 at 11:30
  • Is Villa a dynamic string that should be copied from the old URL to the new? – MrWhite Nov 21 '19 at 11:39
1

If you are "redirecting" old URLs then you should implement an external redirect, not an internal rewrite, as you have done here. (listing?holidaytype=Villa is unlikely to be a valid filesystem path - but that's possibly another issue.)

For example, try the following instead:

RewriteCond %{QUERY_STRING} ^proptype=Villa&action=search$
RewriteRule ^index\.php$ /listing?holidaytype=Villa [R=302,L]

This is a temporary (302) redirect, only change it to a permanent (301) when you are sure it's working OK.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.