Mobile app version of vmapp.org
Login or Join
Smith883

: How do I redirect /search/ to /?s I have a typical Wordpress site, with a typical Search page set up which uses the typical GET format <form id="searchform" action="http://example.com"

@Smith883

Posted in: #Htaccess #ModRewrite #Redirects

I have a typical Wordpress site, with a typical Search page set up which uses the typical GET format

<form id="searchform" action="http://example.com" method="get">
<input id="s" type="text" name="s" placeholder="Search Again">
</form>


For users' convenience, I'd like to have the following url redirect to the search page:
example.com/search?search-term

Redirect to:
example.com/search/search-term

How do I do this?

I need this to work even if the user enters no search parameter. In other words, if the user enters:
example.com/search/

...the displayed URL should remain
example.com/search/

Not the typical
example.com/?s=

...even though the page is still redirecting to the Wordpress search template.

Hope that made sense.

UPDATE: The closest I've been able to get is this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# redirect /search/param to /?s=param
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^search/?$ /?s=%1 [R=302,L]

#standard wordpress stuff
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Smith883

1 Comments

Sorted by latest first Latest Oldest Best

 

@Ann8826881

In order to match against the query string (everything after the first ?) you need to use a RewriteCond directive and check against the QUERY_STRING server variable. The RewriteRule pattern only matches against the URL-path.

So, try something like the following in .htaccess, before any WordPress rewrites:

RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^search/?$ /?s=%1 [R=302,L]


If the user requests /search?[something] then they are redirected. Where [something] is entirely optional.

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



UPDATE: Instead of using the query string to pass the search term (ie. /search?search-term, use the URL-path instead (ie. /search/search-term). And keep the original URL in the browser (ie. don't externally redirect to /?s=) then try something like the following instead:

RewriteRule ^search(/(.*))? index.php?s= [L]


(This replaces both the directives above.)

This allows you to specify either /search, /search/ or /search/search-term and the URL stays in the browsers address bar.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme