3

I'm using FallbackResource directive to route all web requests via front controller /index.php:

FallbackResource /index.php

It works great but one thing bothers me. It's the fact that /index.php is still available directly - same resource has two URL-s:

  • http://www.example.com/resource-path
  • http://www.example.com/index.php/resource-path

I was hoping FallbackResource will deal with this so that only main URL is available because this could pose a SEO problem. What's the best way to fix this?

gseric
  • 131
  • 3

1 Answers1

2

I haven't found a solution in Apache for this. The only solution I've found for this is in handling it in PHP which almost defeats the purpose of using FallbackResource /index.php in the first place.

if (preg_match('#^/index.php#', $_SERVER['REQUEST_URI'])) {
    $redirect = preg_replace('#^/index.php#', '', $_SERVER['REQUEST_URI']);
    if (empty($redirect)) {
        $redirect = '/'; // in case request is domain.com/index.php with no trailing slash
    }
    header('Location: ' . $redirect);
    exit;
}

This is not ideal but it works. It's odd there's so little information about the duplicate URLs issue with FallbackResource. Nevertheless, this solution did not suffice IMO so what I did personally is go back to mod_rewrite.

RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^ %{ENV:BASE}/index.php [L]

Hope this helps you at least. I tried a bounty but nobody seems to have an answer.

Yes Barry
  • 170
  • 1
  • 17