Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Eddie Foreman 215 posts 288 karma points
    Mar 18, 2011 @ 13:29
    Eddie Foreman
    0

    Best approach for 301 redirects

    Hi,

    Looking for some advise on 301 redirects.  Currently moving an existing php based site into Umbraco.  I would need to map the old page to the new page.  Example URLs are:

    http://domain.com/contact.php  to  http://domain.com/contact-us.aspx
    http://domain.com/services.php#products  to  http://domain.com/services/products.aspx

    The hosting provider has asked me to use IIS7 rewrite rules engine, which I've not used and so thought I'd use the Umbraco URLRewriting.config.

    As a test i used:

    <add name="contact"
    virtualUrl="~/contact.php"
    rewriteUrlParameter="ExcludeFromClientQueryString"
    destinationUrl="~/contact-us.aspx"
    ignoreCase="true" />

    This redirected the page, but the url in the browser is contact.php.  Would this still have a status of 301, and how could I rewrite the URL.  Or should I use the IIS7 rewrite rules engine as suggested.  If so could some provide an exampel of how I would perform the rewrites.

    Thanks in advance,
    Eddie

     

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Mar 18, 2011 @ 13:38
    Tom Fulton
    0

    Hi Eddie,

    Not sure off the top of my head about 301 redirects with Umbraco's URLRewriting, but perhaps the easiest way might be to use the excellent 301 URL Tracker package, which among other things, lets you add custom url mappings.

    It's also pretty simple to do in IIS.  I typically create a "Rewrite map" with all my URLs and their new targets, then create a single rule to read this map.  It's a lot easier to do if you have access to the IIS GUI, but in case you don't here's an output from an example web.config.

    <system.webServer>
    ...
    <rewrite>
                <rewriteMaps>
                    <rewriteMap name="MyRewriteMap">
                        <add key="/oldpage.php" value="/newpage.aspx" />
                        <add key="/aboutus" value="/about-us" />
                    </rewriteMap>
                </rewriteMaps>
                <rules>
                    <rule name="Rewrite rule1 for MyRewriteMap" stopProcessing="true">
                        <match url=".*" />
                        <conditions>
                            <add input="{MyRewriteMap:{REQUEST_URI}}" pattern="(.+)" />
                        </conditions>
                        <action type="Redirect" url="{C:1}" appendQueryString="false" />
                    </rule>
                </rules>
            </rewrite>
    ...
    </system.webServer>

    Hope this helps,
    -Tom

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Mar 18, 2011 @ 13:50
    Stefan Kip
    1

    Hi Eddie,

    I created the 301 URL Tracker which Tim mentioned above and it does exactly what you'd like, but there's one problem; nobody seems to be able to map .php/.cfm for example to umbraco yet...

    I'd say, use your current setup and add 'redirectMode=Permanent' as mentioned in this documentation file: http://www.urlrewriting.net/Download.ashx?File=4640f315-27ae-4ab0-8930-6957cfd1f138

  • Eddie Foreman 215 posts 288 karma points
    Mar 18, 2011 @ 16:14
    Eddie Foreman
    0

    Hi Tim and Kipusoep,

    Thanks for your suggestions. It a shame that the 301 URL Tracker does not map .php and other extensions. I tested the URL Config approach with redirectMode=Permanent'.  But the header status gave a reponse of 200, for contact.php.  For SEO I would need this to return a status of 301.

    So I've decided to use the global.asax for the redirects.  Not sure if there are any downside to this approach, but I used it in the past for non Umbraco projects.

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            if (HttpContext.Current.Request.Url.ToString().Contains("http://domain.com/contact.php"))
            {
                HttpContext.Current.Response.Status = "301 Moved Permanently";
                HttpContext.Current.Response.AddHeader("Location", Request.Url.ToString().Replace(
                    "http://domain.com/contact.php",
                    "http://domain.com/contact-us.aspx"));
            }
           
        }

    This returns the desired 301 status.  For this approach I've created a new global.asax file and deleted the App_global.asax.dll.  Remember reading a post, stating thats this does nothing, and so was safe to delete (seems to be OK!!).  If possible it would be good to keep this and get the new global.asax to inherit from it.  Tried, but the redirects need not work.

    Thanks again,

    Eddie

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Mar 18, 2011 @ 16:20
    Stefan Kip
    0

    Well, the 301 URL Tracker can map, as far as I know, anything, including .php. The only problem is; we're not able to let umbraco handle the .php request, so the 301 URL Tracker is able to kick in...

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Mar 18, 2011 @ 16:22
    Tom Fulton
    0

    Hi Eddie,

    The IIS Rewrite rules I posted (should) work with PHP and will do 301's, and might be easier to maintain than a code solution.  But either way should work :)

    -Tom

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Mar 18, 2011 @ 16:24
    Stefan Kip
    0

    I agree with Tom, using the UrlRewrite IIS plugin is better than doing it in-code;
    - Easily adjustable
    - More readable/maintainable
    - Better performance

  • Doug Mackay 56 posts 155 karma points
    Mar 18, 2011 @ 18:50
    Doug Mackay
    0

    In UrlRewriting.config the following should work

    <add name="contact"
        virtualUrl="~/contact.php"
        rewriteUrlParameter="ExcludeFromClientQueryString"
        redirect ="Application"
        redirectMode ="Permanent"
        destinationUrl="~/contact-us.aspx"
        ignoreCase="true" />
  • Eddie Foreman 215 posts 288 karma points
    Mar 18, 2011 @ 19:20
    Eddie Foreman
    0

    Hi Doug,

    Thanks for this.  Almost, the page redirects and the url is updated in the browser.  But the 404 page is displayed?

    Any ideas.

    Thanks,
    Eddie

  • Doug Mackay 56 posts 155 karma points
    Mar 18, 2011 @ 20:32
    Doug Mackay
    0

    Strange.

    So if you go direct to '/contact-us.aspx' the page displays fine but if it's the target for the 301 you get a 404 error?

     

  • Doug Mackay 56 posts 155 karma points
    Mar 18, 2011 @ 23:52
    Doug Mackay
    1

    @kipusoep Thanks for 301 URL Tracker package - it's one of the first packages I install on new sites.

    I've been investigating the issue of getting it to work with .php/.cfm pages :-

    I belive that the umbraco 404 handlers only work with .aspx or extensionless url's (this is hard coded into the umbraco requestModule).

    As a workaround you can set-up the UrlRewritter to rewrite .php requests to a .aspx page that does't exist :-

    <add name="phprewrite"
    virtualUrl="^~/(.*).php"
    rewriteUrlParameter="ExcludeFromClientQueryString"
    destinationUrl="~/php/$1.aspx"
    ignoreCase="true" />

    This then enables you to use the custom mapping in the 301 URL Tracker

    e.g.

    Custom mappings :-

    /php/oldpage.aspx

    /php/olddir/oldpage.aspx

    So /oldpage.php or /olddir/oldpage.php will get 301 redirected to the umbraco page that you've set the custom mapping on.

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Mar 19, 2011 @ 11:26
    Stefan Kip
    0

    That's a great suggestion Doug! Thanks!

  • Eddie Foreman 215 posts 288 karma points
    Mar 19, 2011 @ 16:22
    Eddie Foreman
    0

    Hi Kipusoep and Doug,

    Installed the 301 URL tracker and have set up the rewrtie using Doug's work around for the php files.  Working nicely.

    Thanks again,

    Eddie

  • Craig100 1136 posts 2523 karma points c-trib
    Aug 28, 2013 @ 11:21
    Craig100
    0

    Using V2.2.2 of the 301 Url Tracker on a V6.1.2 site I can quite happily redirect from whatever.php to a nice new Umbraco page/node without any changes to the UrlReWriter.config file. However, one gotcha I came across in the Url Tracker set up is that it strips the ".php" part of the old url you're trying to set in the form when it saves if you don't have "Forward query string" checked. It appears that Url Tracker considers a file extension to be a query string.

    Just thought I'd mention it in case others were head scratching.

    Cheers,

    Craig

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Aug 28, 2013 @ 11:25
    Stefan Kip
    1

    @Craig100
    It should work with or without "Forward query string". Just tested it and it works for me, so it sounds like a setup issue...

  • Craig100 1136 posts 2523 karma points c-trib
    Sep 03, 2013 @ 11:28
    Craig100
    0

    @kipusoep

    You're right! Just redeployed the site as 6.1.5 and removed the check on the "Forward Querystring" boxes and they all worked. What can I say?

    Thanks,

    Craig

Please Sign in or register to post replies

Write your reply to:

Draft