Only domain name visible instead of direct links after the /?

What is the name of the domain?

dddocent.nl

What is the issue you’re encountering

Is it possible to add a rule in Cloudflare to only show my domain name instead of the complete directory after the /? I only want to show dddocent.nl at all times. Can I apply a rule? If so, which one do I need?

What steps have you taken to resolve the issue?

None, because I don’t know if this is possible via a Cloudflare rule?

What are the steps to reproduce the issue?

Creating a rule to only show the domain name (dddocent.nl) at all times.

It is possible to implement this, but the approach depends on your use case. Could you clarify your goal a bit more?

For example:

  1. Single-Page Application (SPA): If you’re using an SPA and want all visitors redirected back to the root (/) regardless of the path they visit, you can use Redirect Rules.
  2. Hiding Folder Structure: If you’re trying to hide the directory structure at the origin by rewriting all paths to /, this could work but requires careful handling. For instance, if your use case requires returning different content for the same path (/), you’ll need to handle this logic:
    2.1 Either through your application at the origin, and / or
    2.2 Your custom JavaScript code in Snippets, and / or
    2.3 By creating specific rules (i.e. URL rewrites) in Cloudflare with clear conditions (e.g., based on referer, headers, country, etc.) to determine how the request should be routed to your origin.

Thanks for your reply. The use case is number 1. I want to hide the url where the domain name is redirected to. It’s redirected here: DDDocent

I only want visitors to see dddocent.nl.

Which rules can I use exactly? Thanks in advance for your help!

Right, so you want to rewrite both the domain name and the URL path. Does origin for dierenoppasdronten.nl accepts and serves requests correctly with the Host header dddocent.nl?

If yes, then first you need to create a CNAME record for dddocent.nl that targets dierenoppasdronten.nl, then add a Rewrite URL rule to append /dddocent/ in front of all URL paths for this hostname (note: you don’t want to rewrite all URLs to /dddocent/index.html because you have embedded images and other assets on that page that won’t load if you point everything to index.html):

  • Request URL: http*://dddocent.nl/*
  • Target path: *
  • Rewrite to: /dddocent${1}

If the origin for dierenoppasdronten.nl doesn’t accept requests with Host header dddocent.nl, you have two options:

  1. If you’re on Enterprise, you can follow Change URI Path and Host Header · Cloudflare Rules docs to deploy this using Transform Rules + Origin Rules. The URL rewrite rule I’ve already provided above, so the only additional step is to add a Host Header rewrite.
  2. Alternatively, if you’re on Pro or above, you can collapse all this logic into Snippets and route all matching requests according to your logic, similar to this example. Your Snippet will look more or less like this:
export default {
    async fetch(request) {
        // First, clone the original request to construct a new request
        const newRequest = new Request(request);
        // Add a header to identify a re-routed request at the new origin
        newRequest.headers.set("X-Rerouted", "1");
        // Clone the original URL
        const url = new URL(request.url);
        // Send request to a different origin / hostname
        url.hostname = "dierenoppasdronten.nl";
        // Append directory to the path
        url.pathname = `/dddocent${url.pathname}`
        // Serve response to the new request from the origin
        return await fetch(url, newRequest);
    }
};

Thanks for your reply. I tried to do everything you said (CNAME, rewrite URL rule), but unfortunately it’s not working. I see that the site is still loading like it was before. Could it be that it takes a while to apply al changes?

The changes are near instant, so I believe this doesn’t work as you expect because your origin serves different content based on the Host header:

  1. If the Host header is not set to dierenoppasdronten.nl, it returns redirect:
% curl -svo /dev/null https://dddocent.nl/dddocent/index.html -H 'Host: dddocent.nl' -k --connect-to ::{redacted}
...
> GET /dddocent/index.html HTTP/2
> Host: dddocent.nl
> User-Agent: curl/8.7.1
> Accept: */*
> 
* Request completely sent off
< HTTP/2 301 
< content-type: text/html
< content-length: 795
< date: Wed, 18 Dec 2024 17:48:55 GMT
< server: LiteSpeed
< location: https://linux2029.webawere.nl/dddocent/index.html
< strict-transport-security: max-age=31536000
< vary: User-Agent
< alt-svc: h3=":443"; ma=2592000, h3-29=":443"; ma=2592000, h3-Q050=":443"; ma=2592000, h3-Q046=":443"; ma=2592000, h3-Q043=":443"; ma=2592000, quic=":443"; ma=2592000; v="43,46"
< 
{ [795 bytes data]
* Connection #0 to host {redacted} left intact
  1. If the Host header is rewritten to dierenoppasdronten.nl, the HTML page is returned with 200 status code:
% curl -svo /dev/null https://dddocent.nl/dddocent/index.html -H 'Host: dierenoppasdronten.nl' -k --connect-to ::{redacted}
...
> GET /dddocent/index.html HTTP/2
> Host: dierenoppasdronten.nl
> User-Agent: curl/8.7.1
> Accept: */*
> 
* Request completely sent off
< HTTP/2 200 
< content-type: text/html
< last-modified: Wed, 18 Dec 2024 09:47:17 GMT
< etag: "55b7-67629a25-c4246e84ceb7167e;;;"
< accept-ranges: bytes
< content-length: 21943
< date: Wed, 18 Dec 2024 17:49:09 GMT
< server: LiteSpeed
< strict-transport-security: max-age=31536000
< vary: User-Agent
< alt-svc: h3=":443"; ma=2592000, h3-29=":443"; ma=2592000, h3-Q050=":443"; ma=2592000, h3-Q046=":443"; ma=2592000, h3-Q043=":443"; ma=2592000, quic=":443"; ma=2592000; v="43,46"
< 
{ [16375 bytes data]
* Connection #0 to host {redacted} left intact

Hence, it’s very likely that you’ll need to follow the steps outlined in this part of my previous message:

Either via Origin Rules or Snippets or Workers (depending on your plan), you can rewrite Host header so that your origin would receive the request in such a way that allows it to return the response you expect.

I’m on the Free plan. So I don’t think I have any options left…?