0

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.

JacobSa
  • 27
  • 6
  • Your current rule is rewriting to `index.php?page=` for every request that doesn't contain a `/` and `.` and is not a directory. You cannot get a regular 404 page with this rule. You will have to think properly why do you need this rule. – anubhava Jan 15 '21 at 06:47

1 Answers1

1

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]
JacobSa
  • 27
  • 6
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/227633/discussion-between-jacobsa-and-ravindersingh13). – JacobSa Jan 21 '21 at 13:01