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://
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="http://{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.
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.
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.
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}
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.)
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"}
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!
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();
}
}
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.