0

I am facing trouble redirecting the keywords to search page in a wordpress 4.4 setup. What I am trying achieve is to rewrite/redirect

www.site.com/**search+keyword.html*** 

to

www.site.com/**?s=search+keyword&submit=Search

I have tried every solution found in google but none works with this version.

.htaccess file:

    <IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
RewriteRule ([^/]+)\.html$ ./?s=$1 [L]

</IfModule>

I can search wordpress using: http://localhost/wordpress/search/keyword, but I am unable to change the search URL.

What I need is to fire a search whenever there is a keyword followed by a .html in the URL. How can I get this done?

0

Don't touch the .htaccess file, there's this neat tool add_rewrite_rule() which you should hook into init:

add_action('init', function() {

    add_rewrite_rule(
        'search+([^/]+).html$',
        'index.php?my_custom_search=1&s=$matches[1]&submit=Search',
        'top'
    );

});

Right after that you should flush the rewrite rules once ( by saving the permalink settings in Settings > Permalinks or with flush_rewrite_rules() BUT call that function only once and when needed ).

This should make www.site.com/search+keyword.html serve search results as www.site.com/?s=search+keyword&submit=Search

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.