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

标签 apache redirect ubuntu laravel-4 virtualhost

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

这是我的默认 Apache VirtualHost 文件:

<VirtualHost *:80>
        ServerAdmin webmaster@example.com
        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。

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

最佳答案

感谢#laravel IRC 聊天中的 anlutro,他指出 mod_rewrite 可能未启用。通常,在我曾经使用过的所有服务器中,默认情况下它都处于启用状态,所以它没有点击,因为我从来没有启用它,但是在这个 DigitalOcean 服务器上,它不是。

我用了:

a2enmod 重写以启用它,然后我重新启动了 apache。一切都很好。

干杯

关于apache - 从 Laravel 4 URL 中删除 index.php 并将 www 重写为非 www,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19380897/

相关文章:

java - 无效距离太远

javascript - 如何防止表单提交后重定向?

javascript - 在脚本中重定向

ubuntu - 运行 rake run:android (Rhomobile) 时出现错误,说明 rm_f 对于 AndroidTools::Module 未定义

ubuntu - 这个错误是什么意思 : "maverick is not supported."

apache - 如何让 Apache 在 Ubuntu 上停止?

python - apache 日志中没有任何内容的 500 错误

javascript - document.location.href 无法在 Safari 2020 上运行

c -/usr/bin/env : ‘python’ : Permission denied---Problem while installing Ninja

apache - 如何仅使用 .htaccess 文件设置 ssl