google-chrome-extensionchrome-extension-manifest-v3chrome-declarativenetrequest

How to redirect an exact URL in ManifestV3 without matching nested pages?


When I specify the site's exact URL as urlFilter in a rule that should redirect to /questions page, it keeps redirecting every page of this site (e.g., profile, specific question, etc.) incorrectly.

[
  {
    "id": 1,
    "priority": 1,
    "action": {
      "type": "redirect",
      "redirect": {
        "url": "https://stackoverflow.com/questions"
      }
    },
    "condition": {
      "urlFilter": "https://stackoverflow.com",
      "resourceTypes": ["main_frame"]
    }
  }
]

Tests

https://stackoverflow.com         | redirected (OK)   |
https://stackoverflow.com/another | redirected (FAIL) | expected: not redirect

How can I formulate in ManifestV3 to redirect only https://stackoverflow.com?


Solution

  • You should be able to use

    [
      {
        "id": 1,
        "priority": 1,
        "action": {
          "type": "redirect",
          "redirect": {
            "url": "https://stackoverflow.com/questions"
          }
        },
        "condition": {
          "urlFilter": "https://stackoverflow.com/|",
          "resourceTypes": ["main_frame"]
        }
      }
    ]
    

    since a | dictates that no more characters may come after it.

    See: chrome.declarativeNetRequest - URL filter syntax - Chrome Extension API