Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tridion 2009 SP1: Is it possible to publish a .htaccess file?

Tags:

tridion

I am using ISAPI rewrite on a project and would like to know if it is possible to publish a .htaccess file from Tridion?

I have tried creating a Page Template with the .htaccess extension but can't create a page with no name.

Any ideas?

Could I use a C# TBB to change the page name?

like image 449
Rob Stevenson-Leggett Avatar asked Apr 13 '12 10:04

Rob Stevenson-Leggett


2 Answers

I would also choose to use a binary to achieve this, but if you want to manage the htaccess file using text, rather than as a multimedia component, you can push a binary into your package using the following technique:

1) Push the text of the Htaccess file into the package with an accessible name (i.e. Binary_Text) 2) Use code similar to the following to create a text file from the text in the variable and add it to the package

class publishStringItemAsBinary : ITemplate
{
    public void Transform(Engine engine, Package package)
    {
        TemplatingLogger log = TemplatingLogger.GetLogger(typeof(publishStringItemAsBinary));
        TemplateUtilities utils = new TemplateUtilities();
        System.IO.Stream inputStream = null;
        try
        {
            string strInputName = package.GetValue("InputItem");
            string strFileName = package.GetValue("strFileName");
            string sg_Destination = package.GetValue("sg_Destination");
            string itemComponent = package.GetValue("mm_Component");

            inputStream = new MemoryStream(Encoding.UTF8.GetBytes(package.GetValue(strInputName)));

            log.Debug("InputObject:" + strInputName);
            log.Debug("Filename for binary:" + strFileName);
            log.Debug("Destination StructureGroup:" + sg_Destination);
            Publication contextPub = utils.getPublicationFromContext(package, engine);
            TcmUri uriLocalSG = TemplateUtilities.getLocalUri(new TcmUri(contextPub.Id), new TcmUri(sg_Destination));
            TcmUri uriLocalMMComp = TemplateUtilities.getLocalUri(new TcmUri(contextPub.Id), new TcmUri(itemComponent));
            StructureGroup sg = (StructureGroup)engine.GetObject(uriLocalSG);
            Component comp = (Component)engine.GetObject(uriLocalMMComp);
            String sBinaryPath = engine.PublishingContext.RenderedItem.AddBinary(inputStream, strFileName, sg, "nav", comp, "text/xml").Url;
            //Put a copy of the path in the package in case you need it
            package.PushItem("BinaryPath", package.CreateStringItem(ContentType.Html, sBinaryPath));
        }
        catch (Exception e)
        {
            log.Error(e.Message);
        }
        finally
        {
            if (inputStream != null)
            {
                inputStream.Close();
            }

        }
    }
}

I think the code is pretty self explanatory. This publishes a binary of type text/xml, but there should be no issue converting it to do a plain text file.

like image 169
Chris Summers Avatar answered Nov 06 '22 22:11

Chris Summers


I think you can use multimedia component to store your .htaccess. Even if you will not be able to upload file without name (Windows limitation), you will be able to change filename later, by modifying BinaryContent.Filename property of multimedia component. You can then publish this component seperately, or use AddBinary method in one of your templates.

There's also a user schema where you can change some other rules: "\Tridion\bin\cm_xml_usr.xsd", but you will not be able to allow empty filenames

like image 3
Andrey Marchuk Avatar answered Nov 06 '22 23:11

Andrey Marchuk