• Hi guys,

    Ive read a few posts on this and never found a clear and concise easy to understand answer.

    What i want to do is change search string URLs from “?s=” to something like /folder/search-term

    Where “/folder” would represent “?s=” and “/search-term” would represent the keywords (currently they would be “search+term”)

    I think this is possible? Obviously something is currently tell wordpress to display searches as “?s=” so I guess it can be changed.

    I dont really want to do it through re-directs though as I want to be done for SEO and indexing reasons primarily as well as it looking nice. It would save me lots of time creating real pages but using my “search.php” to generate the content I need for a large section of the site.

    What I also need it to do is make sure that I could link to the permalinks (hence why re-direct wouldn’t work)

    So right now I could link to “domain.com/?s=search+term” and it would always return the dynamic page. I want it to be able to link to “domain.com/search/search-term” and it return the same results.

    I believe this will take some modification of the WordPress core? Which means id lose it everytime I upgrade correct?

    Either that or I can somehow adapt the “nice search” redirect code and do some .htaccess tricks to make it a permanent change? – perhaps the people who understand will be able to answer that better.

    There are plugins which will automatically make pages from search results for me which save a lot of time but they cost lots of money (for this one simple job) and it would also add them as physically pages or posts in the admin panel when I dont really want to do that (I dont want to have them editable etc I want them to function just the way they do when you search right now)

    Hopefully someone can help!

Viewing 11 replies - 1 through 11 (of 11 total)
  • All you need is a rewrite rule in .htaccess. Add the following on the line after “RewriteBase /”:

    RewriteRule ^search/(.*)$ index.php?s=$1

    That transforms https://blah.com/search/blah into index.php?s=blah.

    Thread Starter djdeejay

    (@djdeejay)

    Thanks Devin,

    But I want to do the opposite…? I want the default search setting of /?s= to appear as if it’s a permalink page on the site as /search/search-term (I want to change the + for a – as well) so google would index the nice looking permalinks instead of the current /?s= it’s indexing now.

    Also isn’t the .htaccess a just simple redirection?

    I think I made a poor choice of words.

    Rewrite rules are not redirection – or at least, not necessarily. They “rewrite” – so in the example I gave above, the address for the page is “blah.com/search/blah”, but (behind the scenes), the user is actually (unknowingly) accessing index.php?s=blah. It’s totally invisible from an end-user point of view.

    So if you added that rule, you could link to domain.com/search/blah, and it would show the search results page for the term “blah”.

    So that rule basically tells your webserver:

    “People are going to be visiting the URL domain.com/search/blah. That file doesn’t exist, but let’s show them index.php?q=blah – WITHOUT them knowing.”

    It seems to me that’s what you want, but maybe I still don’t understand…

    Thread Starter djdeejay

    (@djdeejay)

    Ah yes, that’s what I want.

    So if I make a link directly to /search/blah it’ll do the search?

    Can I now use any search terms after the last trailing slash so…

    /search/red-boots would in default settings have to be linked to /?s=red+boots

    Seems almost too simple as people were writing huge threads on trying to do something similar but not as advanced and using rewrite.php in wo-includes to make the changes. Also this if it works the way I described here works a lot better than the plugin “nice search” – i don’t know why it wouldn’t use this method.

    I’ll test it out in the morning and let you know how I get on…

    Thread Starter djdeejay

    (@djdeejay)

    Hi Devin,

    Ok ive put it in and now I see what you mean and how it works in practice.

    You’re right its not a redirect in that if you still link to /?s= it will work and keep the URL the same, I guess thats a good thing really, it means no extra plugins (like “nice search”, no redirects, no broken links and no features broken when updating the site)

    Now two tweeks – are these at all possible?

    Can I change the folder name from /search/ ? – Id like it to be something else to distance it from the search criteria.

    Secondly can I change the “+” = space, to “-” = space?

    Danny

    OK, here is something I’ve tested, and I think it will work for your purposes.

    In .htaccess, replace what I gave you earlier with:
    RewriteRule ^search/(.+)$ index.php?s=$1&search=true [L]
    “search/” can be replaced with whatever you want the alias to be, so it could be something like this:
    RewriteRule ^whatever-search/(.+)$ index.php?s=$1&search=true [L]
    or
    RewriteRule ^life-universe-everything/(.+)$ index.php?s=$1&search=true [L]
    So for those, you would access, respectively:
    domain.com/whatever-search/some-search-term
    and
    domain.com/life-universe-everything/some-search-term

    Does that make sense? OK, that’s only half of it. In functions.php, located in your theme directory (wp-content/themes/your theme name/functions.php), put the following code at the bottom:

    /**
     * This, in conjunction with a rule in .htaccess,
     * enables us to have a "pretty" search URL (like example.com/search/hello-world).
     */
    add_action('init', 'replace_plus_signs_for_search');
    function replace_plus_signs_for_search()
    {
    	// search=true is set in the .htaccess rewrite rule, to make things simpler.
    	if (array_key_exists('search', $_GET) && $_GET['search'] == 'true')
    	{
    		// if a search term is defined
    		if (array_key_exists('s', $_GET))
    		{
    			// replace dashes with a plus
    			$_GET['s'] = str_replace('-', ' ', $_GET['s']);
    		}
    	}
    }

    So now, if you link to https://example.com/life-universe-everything/this-is-a-term, it will take you to the search results for “this is a term”.

    Hope that helps.

    Thread Starter djdeejay

    (@djdeejay)

    Hi Devin,

    Thanks for your help…seem to nearly be there!

    In theory this should work. In reality my page keeps giving me “no results found” which is if its accessing the search.php file properly it shouldn’t be as thats basically a parsed dynamic file which uses the search term to trigger some plugins to create a unique page.

    Can you give me your site URL so I can look at it?

    Thread Starter djdeejay

    (@djdeejay)

    Hi Devin, could I e-mail it to you?

    Sure – (edit: removed email address)

    MickWP

    (@mickwp)

    Hi djdeejay and Devin,

    I’m looking to accomplish something very similar to where this thread was going, could you give an update as to how you got on please?

    Thank you.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Change default search string "?s=" to permalink structure?’ is closed to new replies.