Help - Search - Members - Calendar
Full Version: Beautifying Search Urls With Mod_rewrite
Movable Type Community Forum > Additional Resources > Tips and Tricks
Culture Snob
I'm trying to use a combination of form output and mod_rewrite to get user-friendly URLs from the MT search function.

Current output, using the "get" method on the form:

http://www.example.com/cms/mt33/mt-search....rch=search+term

Desired URL:

http://www.example.com/search/search+term

The problem is that I don't know how to configure the search form to output the URL as above.

The current search form:

CODE
<form method="get" action="<$MTCGIPath$><$MTSearchScript$>" id="search-form">
<input type="hidden" name="IncludeBlogs" value="1">
                         <input type="text" size="20" name="search" id="search" value="<$MTSearchString encode_html="1"$>" /><br /><input type="submit" value="search"></form>


Alternatively, I'd be (slightly less) happy if I could use mod_rewrite to properly process/direct the following URL (which I can get from the search form):

http://www.example.com/search?q=search+term

My current .htaccess file includes the following rules so that my http://www.example.com/tag/search+term syntax works, and to create a matching http://www.example.com/search/search+term syntax for searches:

CODE
RewriteEngine on
RewriteRule tag/(.+) cms/mt33/mt-search.cgi?tag=$1&blog_id=1
RewriteRule search/(.+) cms/mt33/mt-search.cgi?IncludeBlogs=1&search=$1


Any help would be appreciated.
PRO IT Service
Hi Culture Snob,

Rewrite your form coding from:

CODE
<form method="get" action="<$MTCGIPath$><$MTSearchScript$>" id="search-form">
<input type="hidden" name="IncludeBlogs" value="1">
                         <input type="text" size="20" name="search" id="search" value="<$MTSearchString encode_html="1"$>" /><br /><input type="submit" value="search"></form>


into:

CODE
<form method="get" action="/search/" id="search-form">
<input type="hidden" name="IncludeBlogs" value="1">
                         <input type="text" size="20" name="search" id="search" value="<$MTSearchString encode_html="1"$>" /><br /><input type="submit" value="search"></form>



Good luck,
Mihai Bocsaru
Culture Snob
Thanks for the tip.

But if I make my form this:

CODE
<form method="get" action="/search/" id="search-form">
                         <input type="text" size="20" name="search" id="search" value="<$MTSearchString encode_html="1"$>" /><br /><input type="submit" value="search"></form>


the output is:

http://www.example.com/search/?search=search+term.

No matter what I've tried or where I've looked, I haven't been able to get rid of the question mark, and that seems to cause all sorts of problems (none of which I understand) in mod_rewrite.

Again, ideally the search form would output:

http://www.example.com/search/search+term

Alternatively, I can get the following out of the search form:

http://www.example.com/search?q=search+term

but can't figure out a mod_rewrite rule to properly process it.
PRO IT Service
You're welcome

That's right and I'm afraid you cannot beautifulize it more smile.gif

and that is because of the fact that the search function is based on a form where people enter words they want to search for and this field data is being called via that question mark

any field data is being called via that question mark. since the search script is not php, you cannot put the search value differently than after that automatically inserted question mark

However, for tags since there is no field for visitors to enter tags, but a list of tags displayed you will have beautiful URLs such as: http://www.domain.com/tag/tagname

Enjoy,
Mihai
Culture Snob
So if it's impossible to get the question mark out of the search-form URL output with the "get" method, would it be possible to use the "post" method to somehow call the search string in the URL?

Or how would one compose the mod_rewrite rule to properly process the question mark?

I already have the "beautiful" tag URLs on the Web site, and I"m trying to match the search URLs both for consistency and for tracking local searches.
Culture Snob
So I'm a moron. The literature for mod_rewrite is so far above my head that it's taken me several weeks to realize that the key is a conditional-rewrite rule.

So if I want the server to treat

http://www.example.com/search?q=search+term

as

http://www.example.com/cms/mt33/mt-search....rch=search+term

what would the appropriate rewrite rule be?

Mod_rewrite and regular expressions are baffling to me, but I have an idea that it looks something like this:
CODE
RewriteEngine on
RewriteCond %{REQUEST_URI} /search$
RewriteCond %{QUERY_STRING} q=([^/.]+)
RewriteRule search([^/.]+) cms/mt33/mt-search.cgi?IncludeBlogs=1&search=$1 [L]


I'm not finding any of the tutorials on mod_rewrite terribly helpful. I understand them conceptually, but I can't do anything but guess at the proper regular expressions and syntax to accomplish what I want.

I recognize that this isn't a Movable Type question, but I think people who use the MT tag-beautification tutorial would be interested in doing something similar with their search URLs.

Any help would be appreciated.
Culture Snob
I experimented my way to something that worked.

If anybody has other/better resources for doing this or something similar, I'm sure it would be a help to people who want to trim their search URLs for cleanliness and/or tracking. I had a hell of a time finding mod_rewrite information in an easy-to-understand format, and what I got done was strictly through trial and error.

The issue was the ugliness of search URLs in MT. Specifically, this format:

http://www.example.com/path/to/mt-search.c...rch=search+term

My goal was to get the following format so that it would match clean tag URLs (http://www.example.com/tag/tag+term):

http://www.example.com/search/search+term

(Instructions on beautifying tag links here.)

I wasn't able to accomplish that. What I was able to do was create relatively simple search links using the search form, and then sending the necessary information to mt-search.cgi through mod_rewrite.

When both those things are done, people who type something in get their search results through a URL that looks like this:

http://www.example.com/s?search=search+term

The first step is to add new rules to the .htaccess file:

CODE
RewriteEngine on
RewriteCond %{QUERY_STRING} search=(.+)
RewriteRule (.+) path/to/mt-search.cgi?IncludeBlogs=1&%{QUERY_STRING}


As best I can explain it to my limited understanding, the second line determines if there's a query (marked by a question mark in a URL) that includes "search=". If that condition is true, then it records the query string ("search=" and whatever follows it) and then moves to the third line. The third line directs the server to treat the current URL (http://www.example.com/s?search=search+term) as if it were (http://www.example.com/path/to/mt-search/cgi?IncludeBlogs=1&search=search+term).

At this point, typing in the URL http://www.example.com/s?search=search+term should give you search results.

The next step is to change search forms to output URLs in that format:

CODE
<form method="get" action="/s" id="search-form">
<input type="text" size="20" name="search" id="search" value="" />
<br />
<input type="submit" value="search">
</form>


The key elements of the form are the "get" method, action="/s", and name="search". "Get" calls the URL of the action (http://www.example.com/s) and appends a question mark, the name (search), an equal sign, and the search term that the user enters, resulting in:

http://www.example.com/s?search=search+term
Culture Snob
The method I describe above does cause problems with the MT interface. Because the rewrite rule casts a wide net, it screws up searches within the MT interface. So avoid using it unless you have no use for search functions within Movable Type.
Akotan
You can use some javascript code to force this beautification. Instead passing variables through folders, send users to the definitive pre-formatted url. Just an opinion...

I'm using MT4 and did this rewrite work.
Carlo Laitano
Mark Carey has a plugin to clean up search URLs which works great. Go to www.mt-hacks.com and look for it there. I've been using it for a while now and I love it. No tricky php code or anything
Lateralus
is the mod_rewrite function in your httpd.conf file in apache enabled?

-------------------------
cmsdriven
http://www.thesilvermountain.nl
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.