Home > Back-end >  .htaccess file does not seem to have an effect
.htaccess file does not seem to have an effect

Time:10-21

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?

CodePudding user response:

Considering that path till folder 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]
  • Related