apache.htaccesshttp-status-code-404errordocumentvanity-url

.htaccess Vanity/Nice URLs Causing 404 Error to be Homepage


I recently switched from using this in my .htaccess file

RewriteRule ^example index.php?page=example [L]

To this:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^./]+)/?$ index.php?page=$1 [L,QSA]

The issue is that if I visit something like example.com/euffhfrfreif - it'll just display me my homepage.

I'm trying to configure vanity URLs, so that rather than visitors using example.com/index.php?page=contact, they'll just visit example.com/contact - I've seen this a lot online and it's supposed to be better for Google, etc.

This error doesn't apply to any folders, so visiting example.com/djfhds/fdjdnf will display a 404 error.

How can I get my 404 error back but keep (or keep something similar too) my new system?

Also: I have rewriteengine on and a 404 error page defined in .htaccess.


Solution

  • Based on your shown samples, could you please try following rules. Please make sure you clear your browser cache before testing your URLs.

    RewriteEngine ON
    RewriteRule ^([^./]+)/?$ index.php?page=$1 [L]
    

    The issue is that you are using an else statement in index.php to show your homepage.

    Change your else statement to a 404 error page and make your home page be at index.php?page=home, then add this to htaccess:

    RewriteRule ^([^./]+)/?$ index.php?page=$1 [L]