Search code examples
.htaccessroutesmod-rewriteurl-rewritingapache2

Getting The server encountered an internal error or misconfiguration and was unable to complete your request


I have followed the base project structure from https://github.com/crjoseabraham/php-mvc Using that, I have created a php MVC project that works fine on the development server AWS EC2. I am facing a redirection issue after binding a domain.

I have applied the below changes in my Apache2.conf for a binding domain.

<VirtualHost *:80>
    ServerName subdomain.mydomain.com
    ServerAlias www.subdomain.mydomain.com
    DocumentRoot /var/www/html/project-1/public
    <Directory /var/www/html/project-1/public>

        AllowOverride All
        Require all granted
    </Directory>

ErrorLog ${APACHE_LOG_DIR}/subdomain.mydomain.com_error.log
CustomLog ${APACHE_LOG_DIR}/subdomain.mydomain.com_access.log combined
</VirtualHost>

My project Structure :

enter image description here

The root directory .htaccess file has the below code:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^$ public/ [L]
    RewriteRule (.*) public/$1 [L]
</IfModule>

And the public directory .htaccess code is :

<IfModule mod_rewrite.c>
        Options -Multiviews
    RewriteEngine On
    RewriteBase /project-1/public
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
   
    RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
 
</IfModule>

So, after applying the above changes, I am able to access my project using the domain name and it opens the landing page successfully. But when I click on menu then I am getting the error :

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at [no address given] to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

To resolve this issue, I have applied many changes in a public .htaccess file but was unable to solve it. So, can you please help me to resolve this issue?

Here are changes that I have tried :

RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ public/index.php?url=$1 [QSA,L]

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase //

    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

Apache error log :

AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer: http://subdomain.mydomain.com/


Solution

  • DocumentRoot /var/www/html/project-1/public
    

    Since you have set the DocumentRoot to point directly at the public subdirectory then you should remove the first .htaccess file completely (the one you have called "The root directory .htaccess file" - but it's not "the root", it's in the directory above the root!)

    Then you only need a single .htaccess file inside the public directory (which is "the root") and remove the RewriteBase directive entirely (which you had set incorrectly and would have resulted in a rewrite loop - 500 error).

    For example:

    # The public (root) directory .htaccess file
    
    Options -MultiViews
    
    DirectoryIndex index.php
    
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f   
    RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
    

    The <IfModule> wrapper should also be removed.

    The relative substitution string rewrites the request relative to the directory that contains the .htaccess file. No need for RewriteBase here.