从 Laravel 4 URL 中删除 index.php 并将 www 重写为非 www

问题描述 投票:0回答:1

我尝试了多种方法从我的 URL (http://www.example.com/index.php/login) 中删除 index.php,以及将 www 重定向到非 www URL。

这是我的默认 Apache VirtualHost 文件:

<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName example.com
        ServerAlias www.example.com

        DocumentRoot /var/www/public/
        <Directory />
                Options FollowSymLinks
                AllowOverride All
        </Directory>
        <Directory /var/www/public/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

现在我已将以下内容添加到此文件中:

Redirect 301 / http://example.com

但这并没有奏效,我只是收到一条重定向循环消息。我还有以下 .htaccess:

<IfModule mod_rewrite.c>
    Options -MultiViews
    RewriteEngine On

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

我添加了:

RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

但这也行不通。

另一个问题是,对于我的所有路线,我必须包含index.php,例如http://example.com/index.php/test

就好像我的 .htaccess 文件被忽略了。

服务器是运行 Ubuntu 13.04 x64 的 DigitalOcean Droplet。它有 PHP 5.4.9 和 Apache 2.2.22。

任何帮助将不胜感激。谢谢。

apache redirect ubuntu laravel-4 virtualhost
1个回答
2
投票
我的服务器上未启用

mod_rewrite
。通常,在我使用过的所有服务器中,此功能都是默认启用的,因此它不会单击,因为我从未需要启用它,但在这个 DigitalOcean 服务器上,却没有启用它。

我使用

a2enmod rewrite
启用它,然后重新启动 Apache。

© www.soinside.com 2019 - 2024. All rights reserved.