如何在apache 2.4上使用www访问域

时间:2015-05-16 13:23:01

标签: apache ubuntu laravel

我希望我的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>

关于服务器的东西,我是新手。

2 个答案:

答案 0 :(得分:0)

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

sudo a2enmod rewrite
sudo service apache2 restart

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

127.0.0.1 example.local

答案 1 :(得分:0)

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

127.0.0.1 www.example.com
127.0.0.1 example.com

关于http://www.example.com/contacthttp://example.com/contact工作吗?