skip to Main Content

I have a website that actually has this URL when someone search anything

/search?searchword=KEYWORD&searchphrase=all

and i would like to redirect it to the new ones:

/newsearch?q=KEYWORD

basically, preserve the KEYWORD param and rewrite the URL

I’ve tried RewriteRule ^search?searchword(.*)$ /newsearch?q=$1 [L,QSA] but it doesn’t work, nothing happens.

Thanks.

2

Answers


  1. Could you please try following, based on your shown samples only. You need to catch string after searchword= and before & into back reference(temp memory) to fetch it while doing rewriting url.

    RewriteEngine ON
    RewriteCond %{THE_REQUEST} s/search?searchword=([^&]*)&searchphrase=all[^s&]*s [NC]
    RewriteCond %{REQUEST_URI} !^/newsearch [NC]
    RewriteRule ^(.*)$ /newsearch?q=%1 [NC,L]
    

    When I check this with curl command if this is rewriting correctly or not and it looks fine there.

    curl -IL "http://localhost:80/search?searchword=KEYWORD&searchphrase=all"
    Server: Apache/2.4.46 (Win64) OpenSSL/1.1.1g PHP/7.4.11
    Location: http://localhost/newsearch?q=KEYWORD
    Content-Type: text/html; charset=iso-8859-1
    
    Login or Signup to reply.
  2. Your rule doesn’t work because you are using RewriteRule directive to test querystring. You need to match against %{QUERY_STRING} or %{THE_REQUEST} as mentioned in the answer by @RavinderSingh13.

    RewriteEngine on
    
    RewriteCond %{QUERY_STRING} ^searchword=([^&]+)&searchphrase=.+$ [NC]
    RewriteRule ^search/?$ /newsearch?q=%1 [L,R]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search