Search code examples
.htaccesshttp-redirectmod-rewriteurl-rewritinglocalhost

.htaccess redirects not working on localhost


I have the following .htaccess which is supposed to open /index.php for ALL requests, regardless of whether the requested folder or file exists:

RewriteEngine On
RewriteRule . index.php [END]

Unfortunately, this doesn't work on my localhost:

  • http://localhost:9000/file -> opens /index.php (GOOD)
  • http://localhost:9000/folder/file -> opens /index.php if /folder/ doesn't exist (GOOD)
  • http://localhost:9000/folder/file -> "Not Found" if /folder/ exists (BAD)
  • http://localhost:9000/file.php -> opens the file if it exists and gives a "Not Found" if it doesn't exist (BAD)
  • http://localhost:9000/folder/file.php -> opens the file if it exists and gives a "Not Found" if it doesn't exist (BAD)

My localhost is running on macOS via php -S localhost:9000 in Terminal.


Solution

  • From the comments to the question I understand that you are not using an apache http server for this, but rely on the http server built into php itself (php -S localhost:9000).

    Please note that ".htaccess" style files are apache configuration files. I doubt that the builtin server has any idea about the purpose of those files ...

    Instead that builtin server offers a feature to route all requests through a router script:

    php -S localhost:9000 index.php
    

    This is documented under "Example #3 Using a Router Script" in https://www.php.net/manual/en/features.commandline.webserver.php