dsh1102 2018-09-01 08:32
浏览 137

尽管.htaccess Apache虚拟主机和子域托管VPS,/ index.php仍然存在于2个Laravel应用程序中

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

Site Works when index.php is used in the url

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

enter image description here

 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

enter image description here

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

  • 写回答

1条回答 默认 最新

  • douzao9845 2018-09-03 15:49
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况