Plugin: Redirect 301 (Roll your own URL shorteners!)
Posted: 12 August 2009 07:05 AM   [ Ignore ]  
Summer Student
Total Posts:  5
Joined  05-01-2009

Redirect301 is a MSM compatible plugin that redirects internal links by sending an SEO-friendly 301 header. This tells search engines that your content has moved permanently and avoids penalties for duplicate content.

You can issue a 301 redirect to a new page like this:

{exp:redirect301 url="weblog/template"

If you’re using the Multiple Site Manager, simply specify the name or ID of your site, like this:

{exp:redirect301 url="weblog/template" site="site_short_name_or_id"

If you’re using MSM and you don’t specify the site, redirect301 uses the default_site URL to build redirect links.


UPDATED: Version 1.1 now lets you specify an external URL, like this:

{exp:redirect301 url="http://expressionengine.com" external="yes"

With ‘external=“yes”’, the plugin doesn’t attempt to determine and prepend your site’s URL, so you need the full URL including http://

Use case: short URL system

There are probably several uses for internal 301 redirects, but I’m using it as short URL generator, like this:

1) Create a template group called “go”
2) Edit the index file to include the following code:

{exp:weblog:entries limit="1" entry_id="{segment_2}" disable="member_data|trackbacks|pagination"}
{exp
:redirect301 url="{weblog}/{url_title}"}
{
/exp:weblog:entries} 

This will take a URL such as http://yoursite.com/go/33/ and issue a 301 redirect to the full URL, such as http://yoursite.com/compliments/you-look-nice-today/

Then, to create a link within your posts that readers can share, simply append the post ID to the short URL address, like this:

{exp:weblog:entries} 
<a href="{site_url}go/{entry_id}/">Short URL</a>
{/exp:weblog:entries} 

Optionally, you can add the following code between your <head> tags to help third-party services such as AppSpot’s Revcanonical to determine that you’ve published a short URL. This is good practise but isn’t required for the redirect to work.

{exp:weblog:entries} 
<link rev="canonical" rel="alternate shorter" href="{site_url}go/{entry_id}" />
{/exp:weblog:entries} 

Hope it helps some folks. Your thoughts and suggestions are very welcome.

File Attachments
pi.redirect301.php.zip  (File Size: 2KB - Downloads: 422)
Profile
 
 
Posted: 12 August 2009 08:06 AM   [ Ignore ]   [ # 1 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  743
Joined  02-11-2007

great stuff ! grin

- bjorn

 Signature 

bybjorn.com: ExpressionEngine Freelancer - Premium ExpressionEngine 2.0 Themes - ExpressionEngine Addons @ AddonBakery - contact me on twitter: twitter.com/bjornbjorn - Zerply profile: zerp.ly/bjornbjorn

Profile
 
 
Posted: 14 August 2009 03:30 PM   [ Ignore ]   [ # 2 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  128
Joined  02-13-2008

I implemented like you suggested and it seems like it works real slow

http://simplifiedsafety.com/go/168

Any thoughts on why?

Chris

 Signature 

http://sim.plified.com
skype: c.w.pollock
twitter: cwpollock

Profile
 
 
Posted: 16 August 2009 04:12 PM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  5
Joined  05-01-2009

You could try replacing the code in your /go/ template with this code:

{exp:weblog:entries  limit="1" entry_id="{segment_2}" disable="member_data|trackbacks|pagination|category_fields|categories|custom_fields" dynamic="off"}
{exp
:redirect301 url="{weblog}/{url_title}"}
{
/exp:weblog:entries} 

You might find it runs a little faster that way. Let me know if it’s still slowing down and I’ll do my best to think what could be causing it.

Profile
 
 
Posted: 17 August 2009 07:39 AM   [ Ignore ]   [ # 4 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  128
Joined  02-13-2008

Well I did some playing around with this and the delay is from this statement

header("HTTP/1.1 301 Moved Permanently"); 

Any thoughts on why that would cause a loading delay?

 Signature 

http://sim.plified.com
skype: c.w.pollock
twitter: cwpollock

Profile
 
 
Posted: 17 August 2009 11:31 AM   [ Ignore ]   [ # 5 ]  
Summer Student
Total Posts:  5
Joined  05-01-2009

I’ve racked my brains and can’t think why that line would cause a delay—it’s nearly instant on my test site (not live yet).

Did you try updating the template with the new code as I suggested?

Profile
 
 
Posted: 17 August 2009 11:34 AM   [ Ignore ]   [ # 6 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  128
Joined  02-13-2008

Yeah.. I think it’s a problem with our server configuration. We’re running lighttpd with apache.  I think we’ve isolated that as the issue. If we figure it out I’ll post a comment on what’s happening.

 Signature 

http://sim.plified.com
skype: c.w.pollock
twitter: cwpollock

Profile
 
 
Posted: 17 August 2009 04:18 PM   [ Ignore ]   [ # 7 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  180
Joined  06-27-2007

Hey bud, slick little plugin and a nice advancement on erskine’s shorter.  Anyway you could support
comment_url_title_auto_path ? 
Right now it almost works but you append the site url- if you didn’t I think it would add a lot of flexibility.

Thanks!

 Signature 

twitter

Profile
 
 
Posted: 17 August 2009 05:11 PM   [ Ignore ]   [ # 8 ]  
Summer Student
Total Posts:  5
Joined  05-01-2009

Good idea. I’ve updated the original post with version 1.1 of the plugin—it allows you to use the ‘external’ attribute to specify your own full path, like this:

{exp:redirect301 url="http://expressionengine.com" external="yes"

You could also use this for internal URLs like {comment_url_title_auto_path} if you need to, of course. There’s probably a smarter way I could do this that tests for the presence of ‘http://’ using regex and automatically determines whether the URL is external or internal, but I hope that helps for now.

Profile
 
 
Posted: 26 August 2009 05:01 PM   [ Ignore ]   [ # 9 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  180
Joined  06-27-2007

I love this plugin- using it all over the place. Thanks so much.

 Signature 

twitter

Profile
 
 
Posted: 30 September 2009 03:44 AM   [ Ignore ]   [ # 10 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  400
Joined  03-07-2007

feature request: respect site index page setting
Admin > System Preferences >  General Configuration
Because some sites don’t use complete index.php removal, but only rename the index page
http://expressionengine.com/docs/installation/renaming_index.html

Currently the plugin “assumes” the URL to be from site-root.

I love the idea of this little plugin because of the 301 moved permanently.
The default {redirect=”“} functionality issues a “302, found”.
That in fact says “found”, but moved temporarily under a different URI,
visiting agents/clients SHOULD continue to use the Request-URI for future requests.

edit: it seems the plugin jumps directly after the first redirect statement.
the following code seems to always redirect to the first page ?

{if segment_2="aaa"}
  {exp
:redirect301 url="test/1"}
{if
:else}
  {exp
:redirect301 url="test/never_hit"}
{
/if} 
Profile
 
 
Posted: 30 September 2009 04:27 AM   [ Ignore ]   [ # 11 ]  
Summer Student
Total Posts:  5
Joined  05-01-2009

Good point about the site index page setting. I’ll try to push out an update that fixes that when I get some downtime.

The fact that you can’t use redirect301 in an if statement is to do with the order EE parses templates—it parses {exp:...} tags before it parses conditional statements, so it hits the first exp:redirect301, serves the redirect header, and the if statement never gets a chance to be processed. I don’t know a way around this, but if anyone else does I’d be happy to update the plugin accordingly.

UPDATE: As a potential workaround to the if statement issue, you could do something like this:

{if segment_2="aaa"}
  {embed
="redirect_template1"}
{if
:else}
  {embed
="redirect_template2"}
{
/if} 

It’s slightly messier because you’ll need to put the redirect301 code in a separate template, but it might prove to be a good workaround in a fix. (Not tested it yet, though.)

Profile
 
 
Posted: 24 October 2009 04:59 AM   [ Ignore ]   [ # 12 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  400
Joined  03-07-2007

Found a possible solution thru “solspace performance guidelines” document.
There is a difference between basic conditionals and advanced conditionals.
Advanced (x==y OR x==z) and (if:else) conditionals are parsed after all other ee-tags.
(emphasis on “are parsed” and “after”) http://expressionengine.com/docs/templates/globals/conditionals.html

ee-tags between basic conditionals (if x==y) are only parsed if the condition is met.
(if this can be confirmed, then i can add this info to the comments for the userguide)
(update: doc says only segment, global variables and embed:variables are simple variables)

So the following works, based on the fact that the first redirect that’s parsed, will .. ehm .. instantly redirect:

{if segment_2=="test1"}
  {exp
:redirect301 url="/page/1"}
{
/if}

{if segment_2
=="test2"}
  {exp
:redirect301 url="/page/2"}
{
/if}

{exp
:redirect301 url="/page/other"
Profile
 
 
Posted: 29 October 2009 04:57 AM   [ Ignore ]   [ # 13 ]  
Grad Student
Rank
Total Posts:  74
Joined  10-31-2008

Do you have any advice for setting this up with a custom domain? I’ve used Lessn to customize a short URL, and I’d like to do this again, but I’m not sure how to connect this monster with that one. Is it even possible? Or is there an easier way I’m missing? I’d love for each entry to automatically generate a custom short URL that can be linked to, but I might be totally misunderstanding the purpose of Redirect 301.

Thanks for any help you could give!

Profile
 
 
Posted: 05 May 2010 02:34 PM   [ Ignore ]   [ # 14 ]  
Summer Student
Avatar
Total Posts:  23
Joined  03-24-2009

Excellent plugin, but you should add an if around the switch because it gets called even if the URL value is blank, and I had instances in my template where the value was not sent (I didn’t want redirects on every page).

if($url != ""){
        
switch ($permanent)
        
{
          
case "no":
                    if (
$external=="yes"){
                        Header
"Location:".$url );
                  
}else{
                        Header
"Location:".$site_url.$url );
                  
}
                    
exit();
          default:
            
Header"HTTP/1.1 301 Moved Permanently" ); 
                    if (
$external=="yes"){
                        Header
"Location:".$url );
                  
}else{
                        Header
"Location:".$site_url.$url );
                  
}
                    
exit();
        
}
    } 
 Signature 

Twitter: @the_nerdery
URL: http://nerdery.com

Profile