How to redirect invalid url in .htaccess?

Member

by emelie , in category: SEO , a year ago

How to redirect invalid url in .htaccess?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

Member

by maci , a year ago

@emelie 

To redirect invalid URLs using .htaccess, you can use the following codes:

1
2
3
4
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [L,R=301]


Here's a breakdown of what each line does:

  1. RewriteEngine On: Enables the Apache mod_rewrite module.
  2. RewriteCond %{REQUEST_FILENAME} !-f: This line checks if the requested URL does not point to an existing file.
  3. RewriteCond %{REQUEST_FILENAME} !-d: This line checks if the requested URL does not point to an existing directory.
  4. RewriteRule ^(.*)$ /index.php [L,R=301]: This line redirects any invalid URL to "/index.php" with a 301 (permanent) redirect status.


Replace "/index.php" with the desired URL to redirect invalid URLs to.