Apache Catch All URL


Apache Catch All URL

Typically, Apache handles all the URLs you have configured in its server config file, or virtual host files. However, what about the URLs that you haven’t configured? When users enter a URL not supported on your website, they will see a “404: Page not found” error. Instead, it is advisable to setup a catch all URL, that handles these requests. Let us take a look at how to configure Apache catch all URL for your website/application.

 

How to Configure Apache Catch All URL

Here are the steps to configure Apache catch all URL for your website/application.

 

1. Open Apache config file

Open Apache config file in a text editor. You will find it at one of the following locations:

  • /etc/apache2/httpd.conf
  • /etc/apache2/apache2.conf
  • /etc/httpd/httpd.conf
  • /etc/httpd/conf/httpd.conf

 


$ sudo vim /etc/apache2/httpd.conf

 

2. Setup Apache Catch All URL

Let’s say you have configured Apache to handle all requests to /products, but also want Apache to handle all other URL requests separately, without showing a “404: page not found” message, then add the following Location block in your Apache config file.

<Location ~ "^((?!/product).)*$">
# catch all configuration
</Location>

The above code will handle all requests other than those sent to /products

 

If the above code doesn’t work for you, try the If block inside your Directory block

<If "%{Request_URI} =! '.*/product.*'">
# catch all configuration
</If>

or

<If "%{Request_URI} =! '^((?!/product).)*$'">
# catch all configuration
</If>

 

 

Please Note: If you have setup separate virtual host files for each domain in Apache, then update the virtual host config files, with the ones shown above.

 

3. Restart Apache web server

Restart Apache web server to apply changes


$ sudo /etc/init.d/apache2 start [Debian or Ubuntu]
# sudo apachectl restart [RHEL, CentOS or Fedora]

 

Please ensure that you have cleared the browser cache before you try loading the page again

 

That’s it! Now Apache will automatically redirect all unhandled URLs to your new page, instead of showing “404:page not found” error.

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!