UID

A unique identifier that can be used for SEO-friendly URLs. If defined in the custom type, the UID becomes a mandatory field for the Writer.

You can use all past UIDs to query a given content, to prevent breaking URLs that rely on older UIDs. Use a UID for friendly URLs in pages, blog posts, events,...

A custom type can only define one field of type UID, and its name must be "uid":

Copy
      "uid" : {
  "type" : "UID",
  "config" : {
    "placeholder" : "type a meaningful unique identifier...",
    "label" : "Unique ID"
  }
}
    

Reference

type

string

UID

config

object

objects listed below

config

placeholder

string

Sets an user-friendly placeholder into the field

The UID is unique within a custom type; documents of different types can have the same UID. This is why we add the API ID value of "page" below.

When querying a document by UID, older values will also return your document. This ensure that existing links are not broken when the UID is changed.

Copy
      // Using NodeJS
app.route('/:uid').get(function(req, res) {
  var uid = req.params.uid;
  api(req, res).then(function(api) {
    return api.getByUID("page", uid);
  }).then(function(pageContent) {
    // pageContent is a document, or null if no match
    res.render('index', {
      pageContent: pageContent
    });
  });
});
    
Copy
      $uid = $_GET["uid"];
$response = $api->getByUID("page", $uid);
    
Copy
      // uid is a String
Document doc = api.getByUID("page", uid);
    
Copy
      // UID is a string
Document doc = await api.GetByUID("page", uid);
    
Copy
      // UID is a string
doc = api.getByUID("page", uid)
    

For better search engines ranking (SEO), you may want to redirect old URLs to the latest URL rather than serving the same content on both old and new:

Copy
      // Using NodeJS
app.route('/:uid').get(function(req, res) {
  var uid = req.params.uid;
  api(req, res).then(function(api) {
    return api.getByUID('page', uid);
  }).then(function(pageContent) {
    if(pageContent.uid != uid){
      return res.redirect("/"+pageContent.uid);
    }
    res.render('index', {
      pageContent: pageContent
    });
  });
});
    
Copy
      $uid = $_GET["uid"];
$page = $api->getByUID("page", $uid);
if (is_null($page)) {
    header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
    echo "Not Found";
} else {
    if ($uid != $page->getUid()) {
        header('Location: /?uid='.$page->getUid());
    } else {
        // Render your content here
    }
}

    
Copy
      Document page = api.getByUID("page", uid);
if (page == null) {
  // Return a 404
  response.sendError(HttpServletResponse.SC_NOT_FOUND);
} else if (page.getUid() != uid) {
  // Redirect to the URL corresponding to the actual UID
  response.sendRedirect("/" + page.getUid());
} else {
  // Render content
}

    
Copy
      // Using MVC.NET

public async Task<ActionResult> Detail(string uid)
{
    var api = await prismic.Api.Get(endpoint);
    var doc = await api.GetByUID("page", uid);
    if (doc == null) { // 404
        return new HttpNotFoundResult("Page not found");
    } else if (doc.Uid != uid) { // 301
        return RedirectToActionPermanent ("Detail", new { doc.Uid });
    } else { // 200
        return View (new PrismicDocument (doc));
    }
}
    
Copy
      // Using Rails

@doc = api.getByUID("page", uid)
if (@doc.nil?)
  render inline: "Document not found", status: :not_found
elsif (@doc.uid != uid)
  redirect_to ("/page/" + @doc.uid), status: :moved_permanently  
end
    

It is not obvious why would you need to integrate a UID into your HTML markup, but here it is just in case:

Copy
      p page uid:
  != pageContent.uid
    
Copy
      <p>Page uid: <?= $pageContent.getUid() ?></p>
    
Copy
      <p>Page uid: ${pageContent.getUid()}</p>
    
Copy
      <p>Page uid: @Model.PageContent.Uid</p>
    
Copy
      <p> Page uid: <%= @doc.uid =></p>