1
OS: Mac Monterey
Web Server: Mac built-in Apache server (not MAMP)
Server version: Apache/2.4.54 (Unix)
Server built:   Aug 24 2022 03:08:51

Web site structure:

~Sites/test

In ~/Sites/test/php.index, I have:

echo "Hello World!";

When I do:

localhost/test

I see:

Hello World!

I want everything to go to index.php, no matter what path I enter as part of the url, so I added an ~/Sites/test/.htaccess, with the following in it:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php[L]

When I do:

localhost/test

I still see:

Hello World!

But if I do:

localhost/test/foo

I get:

**Not Found**
The requested URL was not found on this server.

I am following a tutorial, and the .htaccess file is supposed to take me to the index.php file, no matter what path I include in my url.

Any ideas?

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
EastsideDev
  • 6,257
  • 9
  • 59
  • 116
  • I am guessing it is just a typo, but do you really mean "*In `~/Sites/test/php.index, ...`*? – Don't Panic Oct 20 '22 at 23:38
  • Have you [set `AllowOverride`](https://stackoverflow.com/questions/18740419/how-to-set-allowoverride-all) in your main Apache config? – Don't Panic Oct 20 '22 at 23:48
  • 1
    @Don'tPanic, IMHO, dupe attached is talking about how to rewrite in backend, which OP is already very much aware of it. There are issues of htaccess placement OR could be multi/inner htaccess placements along with path mentioning in Rewriting IMHO, thank you. – RavinderSingh13 Oct 21 '22 at 00:48
  • 2
    Must be some confusion. [The dup I linked](https://stackoverflow.com/questions/18406156/redirect-all-to-index-php-using-htaccess) does not mention "*rewrite in backend*", assuming that means something about the PHP routing code? On the contrary, it shows many answers about how to "*Redirect all to index.php using htaccess*", which, AFAICT, is exactly what OP is asking. It also includes all the key parts of your dup answer. This seems like a clear duplicate to me. – Don't Panic Oct 21 '22 at 02:07
  • @Don'tPanic, yes I went through it again and it looks dupe to me. I had requested anubhava to make it a dupe, cheers. – RavinderSingh13 Oct 21 '22 at 08:00

1 Answers1

2

Considering that path till folder/directory Sites is your root path, if this is the case then place your .htaccess rules file along with Sites folder(not inside it, along with it), then try following rules.

Make sure:

  • To use either of rules one at a time only.
  • Clear your browser cache before testing your URLs.

This considers that your path would be something like: /root/singh/hddocs/Sites, /root/singh/htdocs/.htaccess and root/singh/htdocs/Sites/test/index.php.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^  Sites/test/index.php [QSA,L]


OR try following rules if your root is actually starting from Sites folder.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^   /Sites/test/index.php [QSA,L]
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93