We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Plugin: Redirect 301 (Roll your own URL shorteners!)

Development and Programming

Nick C's avatar
Nick C
4 posts
about 16 years ago
Nick C's avatar Nick C

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="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.

       
Bjørn Børresen's avatar
Bjørn Børresen
629 posts
about 16 years ago
Bjørn Børresen's avatar Bjørn Børresen

great stuff ! 😊

  • bjorn
       
cwpollock's avatar
cwpollock
72 posts
about 16 years ago
cwpollock's avatar cwpollock

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

http://simplifiedsafety.com/go/168

Any thoughts on why?

Chris

       
Nick C's avatar
Nick C
4 posts
about 16 years ago
Nick C's avatar Nick C

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.

       
cwpollock's avatar
cwpollock
72 posts
about 16 years ago
cwpollock's avatar cwpollock

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?

       
Nick C's avatar
Nick C
4 posts
about 16 years ago
Nick C's avatar Nick C

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?

       
cwpollock's avatar
cwpollock
72 posts
about 16 years ago
cwpollock's avatar cwpollock

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.

       
eyevariety's avatar
eyevariety
158 posts
about 16 years ago
eyevariety's avatar eyevariety

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!

       
Nick C's avatar
Nick C
4 posts
about 16 years ago
Nick C's avatar Nick C

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.

       
eyevariety's avatar
eyevariety
158 posts
about 16 years ago
eyevariety's avatar eyevariety

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

       
GDmac - expocom's avatar
GDmac - expocom
350 posts
15 years ago
GDmac - expocom's avatar GDmac - expocom

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}
       
Nick C's avatar
Nick C
4 posts
15 years ago
Nick C's avatar Nick C

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.)

       
GDmac - expocom's avatar
GDmac - expocom
350 posts
15 years ago
GDmac - expocom's avatar GDmac - expocom

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"}
       
jpcody's avatar
jpcody
35 posts
15 years ago
jpcody's avatar jpcody

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!

       
The Nerdery's avatar
The Nerdery
16 posts
15 years ago
The Nerdery's avatar The Nerdery

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();
        }
    }
       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.