If your website has invalid URLs like trailing slashes or non-existent paths, it can take users to a blank page and they may immediately move on to another website. When this happens, it also signals the search crawlers that your pages are not found and won’t see the HTTP response code, which will affect your SEO (Search Engine Optimization) rankings. To prevent this, the invalid URLs accessed by users should redirect them to a 404 error page, which can also display a link to another page. If you are using Apache or Nginx web servers and want to redirect invalid pages properly, keep reading to find out how.

Before you implement a 404 redirection, let us understand why this is important:

Why Redirect Invalid URLs to a 404 Page?

Duplicate content: If the invalid URL has a duplicate content of your website, there is a possibility of splitting rankings, which can cause an issue for your search engine rankings. By redirecting these URLs to a 404 page, you can prevent the indexing of duplicate content and increase your SEO rankings.

Broken or Non-existent pages: Without 404 error pages, users may view links that are broken or even pages that don’t exist. A 404 redirection tells users and search engines that the page they are looking for is not available. This will prevent bots from indexing or crawling the invalid pages.

Website cleanliness: After redirecting all the invalid URLs to a 404 error page, you can maintain cleanliness because it removes all the clutter from your website and improves user experience with simple navigation. Having only relevant pages or URLs on your website reduces confusion for search engines and users as well.

Solutions for Apache and Nginx Servers

1.Apache Server Solution

If your website is running on Apache web servers, here is how to redirect to a 404 page:

Step 1: Open the .htaccess file

  • View the root folder of your website
  • Find a file which is named .htaccess

Step 2: Add Rules

These rules need to be added in the .htaccess file to redirect invalid URLs to a 404 page:

RewriteEngine On
# Redirect invalid URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /404.php [L,R=404]
# Define a custom 404 error page 
ErrorDocument 404 /404.html

Step 3: Create a 404.html File

In the same root folder, create a new file with the name 404.html.

Add your custom 404 message like:

<h1>404 - Page Not Found</h1>
<p>Sorry, the page you’re looking for doesn’t exist.</p>

Step 4: Restart Apache

To apply your 404 direction changes, run this:

sudo systemctl restart apache2

Step 5: Test

  • Search for the URL in your browser.
  • 
    https://yourcompany.com/invalid/
  • This should redirect to the 404.html page and show "Sorry, the page you’re looking for doesn’t exist” message.

What do these rules mean?

  • RewriteEngine On: This will turn the rewrite engine on
  • RewriteCond %{REQUEST_FILENAME} !-f: The rule will make sure the request does not match a valid file in your website root folders.
  • RewriteCond %{REQUEST_FILENAME} !-d: This rule will make sure the request does not match a valid directory on your website.
  • RewriteRule ^.*$ /404.php [L,R=404]: This rule redirects the invalid URLs to the 404.php (Error) page. [L] stops the processing, and [R=404] will specify the HTTP 404 status code.

Example

To redirect the pages for trailing slashes on Apache, use this code:

RewriteEngine On
# Redirect trailing slashes on invalid URLs to 404
RewriteCond %{REQUEST_URI} /$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /404.php [L,R=404]

This code is only applied to requests ending with / which do not have a valid directory.

2. Nginx Server Solution

The web server Nginx needs a different way of redirecting the invalid URLs to a 404 page. Here, the location block in the server configuration file is used for redirection. (site-specific config files like nginx.conf).

Use this rule to redirect invalid URLs to a 404 Page:

In you Nginx server block, add this configuration code:

server {
    listen 80;
    server_name yourcompany.com;

    root /var/www/html;

    # Return a 404 for invalid URLs
    location ~* .*/$ {
        try_files $uri =404;
    }

    location / {
        try_files $uri $uri/ =404;
    }
}

Next, create a 404.html File

In the /var/www/html/ , create a 404.html. file

Add this custom message:

<h1>404 - Page Not Found</h1>
<p>Oops! The page you’re looking for doesn’t exist.</p>

Restart Nginx to test the applied changes:


sudo systemctl restart nginx
  • Visit an nvalid URL, for example, https://yourcompany.com/invalidpage/
  • The page should display a custom 404 page.

Explanation of the rules:

  • location ~* .*/$: This rule matches URLs with a trailing slash that does not direct to a valid resource.
  • try_files $uri =404;: This rule will check if the file or directory exists. If the file/directory does not exist, you get a 404 error.
  • try_files $uri $uri/ =404;: Makes sure that all invalid requests (like /services.php/invalidpath/) are redirected to the 404 error page.

Example

To redirect trailing slashes that leads to content that is non-existent:


location ~ ^.+/$ {
    try_files $uri =404;
}

This URL

example.com/nonexistentpath/
then returns a 404 error.

Testing the 404 Redirect Rules

After you configure the rules, test for these URLs to make sure the redirection is working:

  • Search for valid URLs like yourcompany/services.php or yourcompany/about-us/

    These URLs should load normally.

  • Search for Invalid URLs, for example:

    yourcompany/services.php/invalidpath/ or any non-existent page, it should return a 404 error.

You can even use tools like Browser Console which can help you check HTTP status codes by using this command.

cURL Command:

curl -I https://yourdomain.com/invalidpath/

Conclusion

Managing all the invalid URLs on your website with proper redirection will increase your user experience and also make your website SEO-friendly, which ranks it higher on search results. In Apache, you can use the .htaccess rules and for Nginx, you can configure the location blocks and easily redirect invalid URLs to a 404 page.