Loading

How do I upload a file to the domain root for site verification?

Fecha de publicación: Sep 22, 2025
Descripción

Some services require you to place a file in the root of your domain to verify ownership (e.g., Google Analytics). How can this be done in B2C Commerce?

Solución

B2C Commerce doesn't provide a built-in feature for serving a static file in the domain root, but there are two workarounds. This article assumes the file should be accessible at https://www.example.com/foobar.txt


Method 1: Use the Static Mapping feature – will result in a 301 redirect

  1. Upload the file to the following path in your cartridge: /cartridge/static/default/foobar.txt
  2. Add this line to the Static Mappings at Merchant Tools > SEO > Static Mappings:
/foobar.txt s,,,,,/foobar.txt

Requests to https://www.example.com/foobar.txt will redirect to https://www.example.com/on/demandware.static/Sites-YourSiteID-Site/-/-/foobar.txt


Method 2: Customize the RedirectURL-Start controller – no 301 redirect

If redirects aren't allowed, you can serve the file directly by customizing RedirectURL-Start.

 

For a custom cartridge layered on top of the SFRA base cartridge, consider using the following example for RedirectURL-Start:

'use strict';

const server = require('server');
server.extend(module.superModule);

server.prepend('Start', function (req, res, next) {
    // Handle special cases
    const URL_PATH = '/foobar.txt';
    const FILE_PATH = '/foobar.txt';
    const MIME_TYPE = 'text/plain;charset=UTF-8';
    const URLRedirectMgr = require('dw/web/URLRedirectMgr');
    const File = require('dw/io/File');
    const FileReader = require('dw/io/FileReader');
    const redirectOrigin = URLRedirectMgr.getRedirectOrigin();
    if (redirectOrigin && redirectOrigin.indexOf(URL_PATH) === 0) {
        const file = new File(File.STATIC + FILE_PATH);
        if (file.exists()) {
            const fileContent = new FileReader(file).readString();
            res.base.writer.print(fileContent);
            res.base.setContentType(MIME_TYPE);
            const cacheExpiry = Date.now() + (1000 * 60 * 60 * 24); // 24 hours
            res.base.setExpires(cacheExpiry);
            return; // Do not continue the middleware chain
        }
    }

    // Continue with the normal RedirectURL-Start behavior
    next();
});

module.exports = server.exports();

 

The equivalent for SiteGenesis:

'use strict';

var app = require('*/cartridge/scripts/app');
var guard = require('*/cartridge/scripts/guard');

function start() {
    var URLRedirectMgr = require('dw/web/URLRedirectMgr');

    // Handle special cases
    var URL_PATH = '/foobar.txt';
    var FILE_PATH = '/foobar.txt';
    var MIME_TYPE = 'text/plain;charset=UTF-8';
    var File = require('dw/io/File');
    var FileReader = require('dw/io/FileReader');
    var redirectOrigin = URLRedirectMgr.getRedirectOrigin();
    if (redirectOrigin && redirectOrigin.indexOf(URL_PATH) === 0) {
        var file = new File(File.STATIC + FILE_PATH);
        if (file.exists()) {
            var fileContent = new FileReader(file).readString();
            response.writer.print(fileContent);
            response.setContentType(MIME_TYPE);
            var cacheExpiry = Date.now() + (1000 * 60 * 60 * 24);  // 24 hours
            response.setExpires(cacheExpiry);
            return;
        }
    }

    // Standard redirect behavior from app_storefront_controllers RedirectURL.js
    var redirect = URLRedirectMgr.getRedirect(),
        location = redirect ? redirect.getLocation() : null;
    if (!location) {
        response.setStatus(410);
        app.getView().render('util/redirecterrorutil/redirecterror');
    } else {
        app.getView({
            Location: location
        }).render('util/redirectpermanent');
    }
}

exports.Start = guard.ensure([], start);

 

In this example, the content of foobar.txt will be returned at https://www.example.com/foobar.txt directly. The above code assumes that foobar.txt is uploaded to the Organization Static Content folder via WebDAV at /Sites/Static.

Número del artículo de conocimiento

000391650

 
Cargando
Salesforce Help | Article