网站仅在前面使用index.php

时间:2019-05-20 12:53:59

标签: php laravel apache .htaccess

我在Red Hat Linux服务器上有两个网站。第一个位于var/www中,并且在var/www/html/.htaccess上具有以下配置。

Options +FollowSymLinks +ExecCGI

<IfModule mod_rewrite.c>
  RewriteEngine On

 # Reroute to index.php
  RewriteCond $1 !^(index\.php)
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php [L,QSA]

</IfModule>

第二个站点位于var/www/laravel中,其配置文件位于var/www/laravel/public/.htaccess中。我尝试使用与第一个站点相同的配置,删除文件,并使用下面列出的默认配置:

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

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]

我还使用了in the documentation提供的配置:

Options +FollowSymLinks
RewriteEngine On

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

我还检查了我的mod_rewrite是否未对此命令添加注释:

grep -i LoadModule /etc/httpd/conf/httpd.conf | grep rewrite

是。

第一个网站在没有index.php的情况下可以正常运行,但是Laravel需要它。 关于我在做什么错的任何提示吗?

编辑:那就是我为第二个网站创建别名的方式,也许有帮助:

<VirtualHost *:80>
   ServerAdmin example@example.com
   DocumentRoot /var/www/html/
   ServerName www.example.com
   ServerAlias *.example.com
   Alias /laravel /var/www/laravel/public/
   ErrorLog "|/usr/sbin/rotatelogs /var/log/httpd/www.example.com-error_log.%y%m%d 86400"
   CustomLog "|/usr/sbin/rotatelogs /var/log/httpd/www.example.com-access_log.%y%m%d 86400" combined

#   php_admin_value upload_tmp_dir "/var/www/html/tmp/"
#   php_admin_value open_basedir "/var/www/html/:/var/cache/www/"

   <Directory /var/www/html/>
      AllowOverride All
   </Directory>

   <Directory /var/www/laravel/public/>
      AllowOverride All
   </Directory>
</VirtualHost>

1 个答案:

答案 0 :(得分:1)

this answer中介绍了最简单的解决方案。假设您的Apache服务器配置为允许.htaccess个文件,请编辑与您的站点相对应的文件,以包括以下行:

DirectoryIndex index.php

根据Apache documentation

  

当客户端通过在目录名称末尾指定DirectoryIndex来请求目录索引时,/指令设置要查找的资源列表。

如果您的.htaccess文件尚未为index.php指令列出DirectoryIndex,则需要添加它以使Apache知道当基地址为时可以为该文件提供服务。浏览器要求。

如果您不想将服务器配置为允许.htaccess文件,则可以更改站点的httpd.conf文件:

<Directory /path/to/site/>
    DirectoryIndex index.php
</Directory>