How to Use Regular Expressions to Block Websites

Written by usha dadighat
  • Share
  • Tweet
  • Share
  • Email

Regular expressions use special characters, or metacharacters, to describe possible combinations of text in strings. For example, you could create a regular expression that matches all strings that begin with the letter "d" and end with the file extension ".avi." You can use regular expressions with a security appliance device, intrusion protection system or other filtering proxy to block incoming or outgoing website URLs according to conditions that you specify.

Skill level:


  1. 1

    Determine which type of website or file URL that you want to block. Some network administrators block EXE and BIN files in order to keep unwanted programs from installing on machines in the network. You can also choose to block any page from a site domain such as social networking or file sharing site.

  2. 2

    Organise your regular expression(s) using metacharacters. Metacharacters provide instructions on how to sort the letters in the URL string. The "." serves as a replacement for a single character; "c.t" will match "cat," "cut" and "cot" or any string containing "c_t" such as "acute."

    The "|" operates as a logical "or" and parenthesis separate a substring from the rest of the regular expression. The "*" after a character or substring means that a string containing zero or more instances of the section will match while a question mark indicates that a string with zero or one case of the given substring will match.

    Use square brackets to denote a set of acceptable characters; [a-z] matches any lowercase letter. The "^" is used to show the start of a line.

  3. 3

    Create your final regular expression. The regular expression "" will block any YouTube website address. The regular expression "..([Dd][Oo][Cc]|[Xx][Ll][Ss]|[Pp][Pp][Tt])" will block any website address ending with ".doc," ".xls" or ".ppt" and block the download or opening of these files from a web browser. The regular expression "..[bin|exe]" will block any Windows executables ending in ".bin" or ".exe." Use these regular expressions as a blueprint to create any regular expressions you need.

  4. 4

    Edit your settings to add a filtering rule for each regular expression. Procedures differ for each device or system but follow the same basic process. Cisco and H3C are two of the major manufacturers of intrusion protection systems. To add the regular expression to a Cisco device, click "Configuration" from the menu bar of the software, click "Firewall," then "Objects" then "Regular Expressions." Click "Add" on the right side of the pop-up box and enter a name for the regular expression rule and then the expression itself. Click "OK."

    For an H3C device, click "URL Filtering" in the navigation tree and click "URL Policies." Click "Add" and enter the name for the filtering rule. Click "User-Defined URL Rule" and click "Add." Select the "By regular expression" radio button under "Domain Name Filtering" or "URL Filtering." Enter the regular expression in the box and click "Apply." Click "Apply" again to save changes.

    Check the manufacturer's guide for your system to determine the exact process because it is different for each model, although it will likely follow the basic process that Cisco and H3C use.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.