【问题标题】:How to make domain accessible with www on apache 2.4如何在 apache 2.4 上使用 www 使域可访问
【发布时间】:2015-05-16 13:23:00
【问题描述】:

我希望我的 laravel 应用程序可以在网址上使用或不使用 www 访问。像 [http://laracasts.com][1].当我点击http://example.com 时,它起作用了。但是当我点击http://www.example.comhttp://www.example.com/contact 时,我收到了 404 错误。我该如何解决这个问题?我在 Ubuntu 上使用 apache 2.4.7,这是我的 example.com.conf

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com

    ServerAdmin foo@bar.com
    DocumentRoot /var/www/myproject/public

    <Directory /var/www/myproject/public/>
            Options -Indexes +FollowSymLinks +MultiViews
            AllowOverride All
            Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/example-error.log

    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined

    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

这是我的/etc/hosts 文件

127.0.1.1 example
127.0.0.1 localhost
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
xxxx::0 ip6-localnet
xxxx::0 ip6-mcastprefix
xxxx::1 ip6-allnodes
xxxx::2 ip6-allrouters
xxxx::3 ip6-allhosts

.htaccess:

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

RewriteEngine On

# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]

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

关于服务器的东西,我是菜鸟。

【问题讨论】:

    标签: apache ubuntu laravel


    【解决方案1】:

    您的 apache 配置正确。但是,您需要启用重写模块:

    sudo a2enmod rewrite
    sudo service apache2 restart
    

    如果您使用已注册的域,这就足够了。如果您只是使用“本地”域进行开发,则必须将此转储域添加到您的主机文件中/etc/hosts。例如,在本地环境下工作时,我通常使用域.local,我将添加此行以使域example.local 工作:

    127.0.0.1 example.local
    

    【讨论】:

    • 在我运行sudo a2enmod rewritesudo service apache2 restart 之后仍然没有任何反应:(
    • 将这些行添加到您的/etc/hosts 文件中:127.0.0.1 example.com127.0.0.1 www.example.com
    • a,您是否启用了此站点? example.com.conf 是否存在于 /etc/apache2/sites-enabled 文件夹中?
    【解决方案2】:

    您需要在 hosts 文件中包含所有可能的域名/子域名组合。

    127.0.0.1 www.example.com
    127.0.0.1 example.com
    

    关于http://www.example.com/contacthttp://example.com/contact 有效吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      • 2017-10-01
      • 2015-08-31
      • 2014-09-17
      • 2019-07-13
      • 1970-01-01
      相关资源
      最近更新 更多