【问题标题】:/index.php persists in 2 Laravel application despite .htaccess Apache Virtual Host and Subdomain Hosting VPS/index.php 仍然存在于 2 个 Laravel 应用程序中,尽管 .htaccess Apache 虚拟主机和子域托管 VPS
【发布时间】:2019-02-07 03:10:38
【问题描述】:

请,我在删除 index.php 文件时遇到了问题 我的 Laravel 应用程序的路由。 我没有成功纠正它。当我使用服务器的 IP 地址时,一切正常,但是当我使用子域名时,它适用于 / 但对于其他路由或 URL,如 /login,我必须在它工作之前添加 /index.php/login 否则它不会. 以下是我的服务器当前设置或配置的示例。

Site Works when index.php is used in the URL

 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

主要网站

域名.com
Apache CONF 文件(这已经很好用了)

   <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>

第一个子域站点 APACHE 会议:

test1.domain.com

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

// APACHE 配置文件

/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>

//test1.domain.com 的.htaccess

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}]

第二个子域站点 APACHE 会议:

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

/Apache 配置文件 /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 文件配置 选项 -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]

环境

Ubuntu 16.04 
Server PHP 7.2 
Laravel Version 5.4.30

所有这些的结果

 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

请提出要求

我需要摆脱在路由中使用 index.php 的需要。 拜托,欢迎任何想法。 也许新鲜的眼睛会帮助我找到解决这个问题的方法。

我所做的研究 我已经尝试过以下链接。仍然很难找出问题所在以便纠正。

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

再来一次。 FIX 消除在 Web 应用程序路径中包含 index.php 的需要。提前致谢

【问题讨论】:

    标签: php laravel apache .htaccess vps


    【解决方案1】:

    我能够解决这个问题。

    首先:.htaccess 文件没有被读取。 尽管我在上面做了所有这些,但在我遵循 通过在 .htaccess 文件中放置乱码并在下面的链接中进行说明 是没有500状态错误显示

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

    但它仍然无法正常工作。 我开始检查 /etc/apache2/site-enabled 目录下的所有文件 并发现因为我在设置 Laravel 应用程序之前在我的服务器上安装了 SSL Encrypt,所以使用的配置文件是 Encrypt 为在

    找到的域生成的配置文件

    /etc/apache2/sites-enabled/test1.domain.com-le-ssl.conf(ssl - 生成的配置)

    /etc/apache2/sites-enabled/test1.domain.com.conf(正常配置)

    /etc/apache2/sites-enabled/test2.domain.com-le-ssl.conf(ssl 生成的配置)

    /etc/apache2/sites-enabled/test1.domain.com.conf(正常配置)

    还有

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

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

    所以我查看了文件,发现 Document Root 指向不同的 目录中的路径,这就是未读取 htaccess 文件的原因,但是 当我使用指向第二个子域的 IP 地址时被读取

    请注意,这是在 SSL APACHE 配置文件中,而不是在正常的 APACHE 配置文件中 DocumentRoot /var/www/test1.domain.com/public_html

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

    总之,这是修复它的最终更新示例

    <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>
    

    结果

    我的路由和 URL 按预期工作。因为 .htaccess 文件现在被 apache 在适当的目录中正确读取

    课程学习

    1. 在安装 Encrypt 之前配置您的 Apache 文件,因为它会复制文件并添加必要的配置以确保其安全。
    2. 或交叉检查/双重检查正常和 SSL 配置文件,因为您肯定希望您的站点得到保护并按预期工作。
    3. 仔细检查 DocumentRoot 和 Directory,确保它们指向相同的目标或按预期指向。

    这些是我从所有这些中吸取的教训。希望其他人从中受益。谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-26
      • 1970-01-01
      • 2012-09-15
      • 1970-01-01
      • 2020-02-09
      • 2015-05-27
      • 2013-12-14
      • 1970-01-01
      相关资源
      最近更新 更多