Help - Search - Members - Calendar
Full Version: How To Make Tag Links With Apache Rewriting?
Movable Type Community Forum > Using Movable Type > Templates and Tags
snow
About the new tag feature, the user manual said:

QUOTE
The search link will look something like:

http://example.com/mt/mt-search.cgi?blog_i...mp;tag=politics

Using Apache rewriting, the search URL can be cleaned up to look something like:

http://example.com/tag/politics



Is there any tutorial about how to setup with Apache rewriting to change the look of that search URL?

Also, my blog is in subdomain, like: http://somebody.example.com/tag/politics

Thanks
justG
The instructions were posted in a beta blog entry:

Beautifying the tag search link URLs

Since the tag search is done using Movable Type's external search, the URLs are not what one might call pretty. Your typical MTTagSearchLink looks like this:

http://example.com/mt/mt-search.cgi?tag=po...s&blog_id=1

If you're using Apache for your webserver, you can make them much nicer looking by using a Apache's built in URL rewriting engine.

http://example.com/tag/politics

Provided your server allows .htaccess overriding (note: most do), you can add the following code to the top of the .htaccess file in your blog's Site Root directory. You may need to create this file if it doesn't exist:

RewriteEngine on
RewriteRule tag/(.+) /mt/mt-search.cgi?tag=$1&blog_id=30

Then, you can link to tags inside of an MTTags or MTEntryTags loop using the following template code:

<a href="/tag/<$MTTagName encode_url="1"$>"><$MTTagName$></a>

Of course, if you're using a Movable Type Index template to create your .htaccess file, you can replace the hard-coded blog ID with <$MTBlogID$> and then simply use that same template code on all of your blogs.
snow
Thank you for the link justG.
I took a quick modification, but didnt suceed, I'll look into it carefully later.
justG
If you'd like to post what you've put in your .htaccess file, I can take a look. I'm no expert, but maybe we can muddle through it together. =)

.g
snow
Thank you justG, let me try to explain what I did.

My MT was installed under: http://www.example.com/cgi-bin/mt
My blog URL is: http://blog.example.com/

========================

My .htaccess is very simple, I just put the following code into it, nothing else.

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


"14" is my blog ID.

I uploaded .htaccess as: http://blog.example.com/.htaccess

==========================

In template, I put:

CODE
<a href="/tag/<$MTTagName encode_url="1"$>"><$MTTagName$></a>


to where I want the tag link to appear.


=============================

At the user end, when the mouse hover, the tag link URL appears to http://blog.example.com/tag/my_tag_name, which is correct and nice.

But the link can not be clicked through, it will give me 404 error.

I guess because my blog URL is subdomain, the second line in .htaccess is not correct.
I tried to put several different thing into this line, such as "/../cgi-bin", but no luck.


huh.gif I guess my objective is to find what should I put in .htaccess?


================================

I also tried to write it this way:

CODE
RewriteEngine on
RewriteRule tag/(.+) http://www.example.com/cgi-bin/mt/mt-search.cgi?tag=$1&blog_id=14


Well! Now the link works! but ohmy.gif ...... after clicked, the URL in address bar will be:
CODE
http://www.example.com/cgi-bin/mt/mt-search.cgi?tag=my_tag_name&blog_id=14


This looks like I didnt beautify the tag search link at all.
justG
Do you have any other rewrite rules in your .htaccess file, or this the only one?

- Gita
snow
That's the only .htaccess file related to MT installation and blog files.
I used to setup a customized error message by adding:

CODE
ErrorDocument 404 /notfound.php


in .htaccess
as: http://blog.example.com/.htaccess

It works.


Altogether there are 3 lines:

CODE
ErrorDocument 404 /notfound.php
RewriteEngine on
RewriteRule /tag/(.+) /cgi-bin/mt/mt-search.cgi?tag=$1&blog_id=14
justG
Hi snow, you want to PM me your actual domain and we can have a look at it? Better yet, if you check my profile, there are several ways to IM me. Please feel free to do so. If we can figure it out, you can update this thread later to help anyone else with the same question in the future.

[Edit] Hang on, I just noticed something. In my RewriteRule, I have "tag/" not "/tag/" -- did you try that? Oh, also, in my template, instead of href="/tag/..." I have:

CODE
<a href="<$MTBlogRelativeURL$>tag/...">


See if that helps?

.g
snow
Thank you G. My real site hasnt finished the upgrading process (plugins compitible issue), I can not rebuild the entries right now. So I just setup a test blog (also in a subdomain) to test all this.

My latest shot seems making some progress:

in template:
CODE
<a href="http://www.example.com/tag/<$MTTagName encode_url="1"$>"><$MTTagName$></a>



in .htaccess:
CODE
RewriteEngine on
RewriteRule tag/(.+) /cgi-bin/mt/mt-search.cgi?tag=$1&blog_id=14



The result will be:
http://www.example.com/tag/my_tag_name


I'd like to make it as: http://blog.example.com/tag/my_tag_name

I tried to learn the online instruction for URL Rewriting here, but it's too hard for me.
I tried a lot of combinations, nothing works. Feel frustrated sad.gif


If anyone else has exprience about this, please help. I would be really appreciated.
zeguigui
I have the same issue but this is an Apache issue really.

My virtual host configuration file contains those instructions:
CODE
RewriteEngine on
RewriteRule ^/weblog/tag/(.+) /cgi-bin/mt33/mt-search.cgi?tag=$1&blog_id=1  [R,L]


The R makes this work like adding http://www.example.com (Redirect) and L means Last as I do not need to parse any other rewrite rule.

Why R is needed in our case and not for someone else? Well it depends on your hosting directory structure. For instance if I have:

/var/www/cgi-bin -> cgi files
/var/www/html -> website

Then I will probably need the [R] instruction. If cgi-bin is a subdir of my hosting directory then it will not be necessary. The reason is found in apache error_log file (and documentation!).

When I do http://www.example.com/weblog/tag/foo it will be rewriten as /var/www/html/cgi-bin/mt33/mt-search.cgi?tag=foo&blog_id=1 whereas it should be /var/www/cgi-bin/mt33/mt-search.cgi?tag=foo&blog_id=1.

What I still have to test:
- put the directive in a per-directory context
- try using RewriteBase to force /var/www/cgi-bin

You can see what happens on my website.

My template is not yet updated to use the "tag url syntax"
justG
Hi snow, did you get this url rewriting malarkey sorted? Sorry, I got busy with some stuff and had to take a break from forums 'n' stuff for a while.

----------------------------
in template:
CODE
<a href="http://www.example.com/tag/<$MTTagName encode_url="1"$>"><$MTTagName$></a>



in .htaccess:
CODE
RewriteEngine on
RewriteRule tag/(.+) /cgi-bin/mt/mt-search.cgi?tag=$1&blog_id=14



The result will be:
http://www.example.com/tag/my_tag_name


I'd like to make it as: http://blog.example.com/tag/my_tag_name
----------------------------

Okay, so... what happens if you change the href to href="http://blog.example.com/tag/..." ? I'm thinking that probably wouldn't work, but I'd probably try it anyway. =) You're right, this sounds like it should be a really simple problem, the solution to which is (was?) eluding us because apparently neither one of us knows enough about rewriting URLs, regular expressions, or .htaccess. Heh. Hopefully you've sorted this already, and if you have, it'd be great if you could update this thread.

.g
JoshuaKaufman
Hello. I have a related question. I would like to use the following URL structure to get to my tag search results:

CODE
/archives/tags/tagname


and I'm able to do that using the following lines in my .htaccess:

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


However, I also want requests for the page:

CODE
/archives/tags/index


to not be rewritten. As you can see here, it's currently using the above RewriteRule to search for the tag 'index' when there isn't one. I simply want it to load the page named index in that directory. Does anyone know how I can do this using .htaccess?
JoshuaKaufman
Thanks to MT support, I found out how to do it:

CODE
RewriteEngine on
RewriteRule tags/index.* - [L]
RewriteRule tags/(.+) /mt/mt-search.cgi?tag=$1&blog_id=2
J. Cormier
This rewrite works successfully for me to change the link that displays in the status bar when you mouse over a link, but once you reach the actual page the URL displayed in the address bar in the browser is the same ugly underlying search link as usual. Any ideas?
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.