简体   繁体   中英

/index.php persists in 2 Laravel application despite .htaccess Apache Virtual Host and Subdomain Hosting VPS

Please, I have been having issues with eliminating index.php file in the routes for my Laravel applications. I have not had success rectifying it. When I use the server's IP address everything works fine but when I use the sub-domain names it works for / but for other routes or URL like /login, I must add /index.php/login before it works otherwise it doesn't. Below is a sample of my server current settings or configurations.

Site Works when index.php is used in the URL

在URL中使用index.php时,站点可以工作

 Site doesn't work when index.php is removed from the Url

在此处输入图片说明

 But it works well when i use the IP address directly. 
 but in this case it serves the laravel application hosted in the second subdomain. I just don't know why

在此处输入图片说明

MAJOR SITE

domain.com
Apache CONF FILE (This Works Great Already)

   <VirtualHost *:80>
    ServerAdmin admin@domain.com
    ServerName domain.com
    ServerAlias www.domain.com
    DocumentRoot /var/www/domain.com/public_html

    <Directory /var/www/html/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <IfModule mod_dir.c>
        DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
    </IfModule>

     RewriteEngine on
     RewriteCond %{SERVER_NAME} =www.domain.com [OR]
     RewriteCond %{SERVER_NAME} =domain.com
     RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
  </VirtualHost>

FIRST SUBDOMAIN SITE APACHE CONF:

test1.domain.com

/var/www/test1.domain.com/public_html

// APACHE CONF FILE

/etc/apache2/sites-available/test1.domain.com.conf

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName test1.domain.com
    ServerAlias www.test1.domain.com
    DocumentRoot /var/www/test1.domain.com/public_html

    <Directory /var/www/test1.domain.com/public_html/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <IfModule mod_dir.c>
        DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
    </IfModule>

   RewriteEngine on
   RewriteCond %{SERVER_NAME} =test1.domain.com
   RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
  </VirtualHost>

//.htaccess for test1.domain.com

IfModule mod_rewrite.c>
 <IfModule mod_negotiation.c>
    Options -MultiViews
 </IfModule>

RewriteEngine On

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

SECOND SUB-DOMAIN SITE APACHE CONF:

test2.domain.com /var/www/test2.domain.com/public_html

/APACHE CONF FILE /etc/apache2/sites-available/test2.domain.com.conf

  <VirtualHost *:80>
    ServerAdmin admin@test2.domain.com
    ServerName test2.domain.com
    ServerAlias test2.domain.com
    DocumentRoot /var/www/test2.domain.com/public_html

    <Directory /var/www/test2.domain.com/public_html/>
       Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <IfModule mod_dir.c>
        DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
    </IfModule>

    RewriteEngine on
    RewriteCond %{SERVER_NAME} =test2.domain.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
   </VirtualHost>

.htaccess

test2.domain.com htacess file config Options -MultiViews -Indexes

RewriteEngine On

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

Environment

Ubuntu 16.04 
Server PHP 7.2 
Laravel Version 5.4.30

OUTCOME OF ALL THESE

 domain.com //WORKS

 test1.domain.com // WORKS
but test1.domain.com/login  // Does'nt work
instead test1.domain.com/index.php/login // works

test2.domain.com // WORKS
but test2.domain.com/login  // Does'nt work
instead test2.domain.com/index.php/login // works

but I realized that
the Ip address works well without the need for index.php
http://server-ip-address // works but serves the app in test2.domain.com
http://server-ip-address/login  // also Works well which is what I expect in the two subdomain test1.domain.com and test2.com

REQUEST PLEASE

I need TO GET RID OF THE NEED TO USE index.php in the route. Please, Any Idea is welcome. Perhaps fresh Eyes would help me identify ways to fix this out.

RESEARCH I HAVE made I Have tried the following links already. still having a hard time finding out what is wrong so that it is rectified.

htaccess for domain and subdomain with laravel htaccess for domain and subdomain with laravel

apache virtual host and "Dynamic" Domains

Setting document root for Laravel project on Apache virtual host apache virtual host and "Dynamic" Domains

ONCE AGAIN. FIX To Eliminate the need to include index.php in the Web APPLICATION ROUTE. THANKS IN ADVANCE

I was able to fix the issue.

First: The .htaccess files were not being read. despite all that I did above, this was confirmed after I followed the instructions in the link below by placing gibberish in the .htaccess files and there was no 500 status error display

.htaccess file not being read ("Options -Indexes" in .htaccess file not working)

But still it wasn't working. I began to check all the files in the /etc/apache2/site-enabled directory and discovered that because I installed SSL Encrypt On My Server Prior To Setting up the Laravel apps, the config files being used are the ones generated by Encrypt for the domains found at

/etc/apache2/sites-enabled/test1.domain.com-le-ssl.conf (ssl - generated config)

/etc/apache2/sites-enabled/test1.domain.com.conf (normal config)

and

/etc/apache2/sites-enabled/test2.domain.com-le-ssl.conf ( ssl -generated config)

/etc/apache2/sites-enabled/test1.domain.com.conf (normal config)

and also

/etc/apache2/sites-enabled/domain.com-le-ssl.conf

/etc/apache2/sites-enabled/domain.com.conf

So I viewed the files and discovered that Document Root pointed to a different path from the Directory that was why the htaccess files were not being read but were being read when I used the IP address that pointed to the second subdomain

NOTE THIS IS IN THE SSL APACHE CONFIG FILES NOT THE NORMAL APACHE CONFIG FILES DocumentRoot /var/www/test1.domain.com/public_html

<Directory /var/www/html/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

In Summary this is the final update sample that fixed it

<IfModule mod_ssl.c>
   <VirtualHost *:443>
     ServerAdmin admin@test1.domain.com
     ServerName test1.domain.com
     ServerAlias www.test1.domain.com
     DocumentRoot /var/www/test1.domain.com/public_html

    <Directory /var/www/test1.domain.com/public_html/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <IfModule mod_dir.c>
        DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
    </IfModule>

  Include /etc/letsencrypt/options-ssl-apache.conf
  SSLCertificateFile /etc/letsencrypt/live/test1.domain.com/fullchain.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/test1.domain.com/privkey.pem
  </VirtualHost>
 </IfModule>

OUTCOME

My Routes and URL Worked As I Expected. as the .htaccess files were now properly being read by apache within appropriate directories

LESSONS LEARNT

  1. Get Your Apache Files Configured Before installing Encrypt as it would duplicate the files and add the necessary configurations to make it secured.
  2. Or Cross Check/Double check the normal and the SSL Configuration files as well because you definitely want your site to be secured and working as expected.
  3. Double check the DocumentRoot and the Directory and ensure that they point to the same destination or as expected.

Those are my lessons from all These. Hope Someone else benefits from this. Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM