Would you like to learn how to install Apache and deny access to URLs with specific query strings? In this tutorial, we are going to configure the Apache server to block the access to URL with a specific query string.

• Ubuntu 20
• Ubuntu 19
• Ubuntu 18
• Apache 2.4.41

In our example, the Apache server is hosting the website WWW.GAMEKING.TIPS.

Equipment list

The following section presents the list of equipment used to create this tutorial.

As an Amazon Associate, I earn from qualifying purchases.

Tutorial Apache - Blocking a URL with specific query string

Install the Apache server.

Copy to Clipboard

Enable the required Apache modules.

Copy to Clipboard

Edit the Apache configuration file for the default website.

Copy to Clipboard

Add the following lines to this configuration file.

Copy to Clipboard

Change this line, enter the words that you want to deny access in the URL query.

Copy to Clipboard

Here is the file, before our configuration.

Copy to Clipboard

Here is the file, after our configuration.

Copy to Clipboard

Restart the Apache service.

Copy to Clipboard

In our example, the Apache server will deny access to any URL with queries containing the following words.

Copy to Clipboard

From a remote Linux computer, try to access a blocked URL.

Copy to Clipboard

Here is the command output.

Copy to Clipboard

If a user tries to access a URL with a query containing the prohibited words, the request will be denied.

Congratulations! You are able to deny access to URLs with a query containing specific words on the Apache server.

Tutorial Apache - Blocking a URL with specific query string using HTACCESS

Install the Apache server.

Copy to Clipboard

Enable the required Apache modules.

Copy to Clipboard

Edit the Apache configuration file.

Copy to Clipboard

Add the following lines at the end of the file.

Copy to Clipboard

Create an HTACCESS file on the website directory.

Copy to Clipboard

Add the following lines to this configuration file.

Copy to Clipboard

Change this line to reflect your needs.

Copy to Clipboard

Configure the correct file permission.

Copy to Clipboard

Restart the Apache service.

Copy to Clipboard

In our example, the Apache server will forbid access to URLs with a query containing the following words.

Copy to Clipboard

From a remote Linux computer, test your configuration.

Copy to Clipboard

Here is the command output.

Copy to Clipboard

Congratulations! You are able to deny access to URLs with a query containing specific words on the Apache server using an HTACCESS file.