3

I have searched StackOverflow for my answer, but nothing that I have seen seems to work.

I have a framework that sends all requests to an index.php file. Everything works when I install it in the root of the virtual host:

http://example.com/
http://example.com/home
http://example.com/home/index

The problem happens when I try to install the framework in a subdirectory like:

http://example.com/blog/

Requests like:

http://example.com/blog/home
http://example.com/blog/home/index

All of those requests should be sent to the index.php file that lives in /blog

My current set up is:

http://example.com/index.php - This file just prints out "we are in the root of the virtual host". The framework is not installed there.

http://example.com/blog - This works fine

When I try to get to http://example.com/blog/home I get the root index.php file, not the frameworks. I get the message "we are in the root of the virtual host".

I have the following .htaccess file located at http://example.com/blog/.htaccess.

RewriteEngine on

Options Indexes FollowSymLinks -MultiViews

RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php

Any help is greatly appreciated.

4 Answers 4

2

The answer that fixed my problem is:

RewriteEngine On

Options Indexes FollowSymLinks

RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php

It appears that the Multiviews was throwing it off.

2
  • It was the fact you were mixing arguments with and without a - (or +) prefix in The Options directive that would have likely caused a problem. As stated in the Apache 2.2 docs: "Mixing Options with a + or - with those without is not valid syntax and is likely to cause unexpected results.". Alternatively, you could have written Options +Indexes +FollowSymLinks -MultiViews - but this is then adding to (or removing) what has already been set in the server config, rather than overriding it.
    – MrWhite
    Aug 6, 2020 at 9:51
  • And don't forget the L flag on the RewriteRule directive, should you add any more directives.
    – MrWhite
    Aug 6, 2020 at 9:55
0
RewriteBase /blog

Try it without the forward slash.

1
0

Are the requests that you're testing with for items that don't actually exist in the directory? These lines:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

Mean that any requests for a file or directory that exists won't be rewritten.

1
  • The directory does not exist, but could.
    – manumoomoo
    Mar 11, 2010 at 21:34
0
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php

Means "if not js or ico..." forward to index.php.

So, in essence, your /blog level .htaccess is saying if it's not an image or CSS file redirect it to /blog/index.php.

2
  • This is what I want. I want all requests, excepted those piped, to go to the index.php page. If I go to domain/blog this works. When I go to domain/blog/home it goes to the domain this is the problem.
    – manumoomoo
    Mar 11, 2010 at 16:30
  • This is simply an optimisation. The first RewriteCond directive will otherwise exclude all requests that map to the filesystem.
    – MrWhite
    Aug 6, 2020 at 10:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .