I want my laravel app accessible with or without www on the url. like [http://laracasts.com][1]. when I hit http://example.com it works. But when I hit http://www.example.com, or http://www.example.com/contact I'm getting 404 error. How can I solve this? I'm using apache 2.4.7 on Ubuntu and here's my 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>
and here's my /etc/hosts file
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
and .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>
about server stuff, i'm noobs.
Your apache configuration is correct. However, you need to enable the rewrite module:
sudo a2enmod rewrite
sudo service apache2 restart
If you use a registered domain, this is enough. If you just use a "local" domain for development, you have to add this dumping domain into your hosts file at /etc/hosts. For example, when working with local environment, I usually use a domain .local, I'll add this line to make the domain example.local work:
127.0.0.1 example.local
You will need to include all the possible domain/subdomain name combinations that you would like to use in your hosts file.
127.0.0.1 www.example.com
127.0.0.1 example.com
In regards to http://www.example.com/contact does http://example.com/contact work?
Related
Using Ubuntu 16.04 and Apache virtual hosts.
I have a Wordpress site: www.example.com
Other sites commonly link to URLs such as:
sub1.example.com/page1
sub2.example.com/page2
I want to redirect them to:
example.com/page1
example.com/page2
I have tried:
RewriteCond %{HTTP_HOST} ^sub1.example.com$
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
and
RewriteCond %{HTTP_HOST} ^sub1\.example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
With both of these configurations, I get the following behaviour:
sub1.example.com goes to example.com as intended
sub1.example.com/page1 goes to a generic Apache 404 page "The requested URL was not found on this server." with the URL sub1.example.com/page1, it is not redirecting
My DNS setup involves A records pointing at the server IP for example.com, www, sub1 etc.
My virtual host .conf file looks like this:
<VirtualHost *:80>
ServerAdmin webmaster#example.com
DocumentRoot /var/www/example.com/public_html
ServerName example.com
ServerAlias www.example.com
<Directory /var/www/example.com/public_html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<IfModule mod_dir.c>
DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
</IfModule>
</VirtualHost>
I have tried adding sub1.example.com as a ServerAlias and reloading Apache to no avail.
How might I solve this?
I fixed the issue by creating individual virtual hosts for each subdomain, each with its own files structure and .htaccess file.
I am running a website on Ubuntu, apache server and my website is running fine till last night and it suddenly start giving not secure error i.e when accessing using https it is giving error (ssl was installed 1 day back only). When accessing without http it is redirecting to https.
So I reinstall my ssl certificate from certbot, and I did not select all redirect traffic to https. I thought there is some configuration error with ssl so http should work. But issue did not resolve.
Then I thought issue is with my configuration so I checked my logs and I found some errors like -
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
AH01909: 127.0.1.1:443:0 server certificate does NOT include an ID which matches the server name
AH01630: client denied by server configuration: /var/www/html/
Such errors are there in my error logs, I am using one virtual host and I have disable the default conf in site-available.
My virtual host file content is
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/domain
ServerName domain.com
ServerAlias www.domain.com
<Directory /var/www/domain/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<IfModule mod_dir.c>
DirectoryIndex home.php index.pl index.cgi index.html index.xhtml index.htm
</IfModule>
RewriteEngine on
RewriteCond %{SERVER_NAME} =domain.com [OR]
RewriteCond %{SERVER_NAME} =www.domain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
In place of domain I am using my original domain name.
This is my /etc/hosts entry
127.0.1.1 domain.com domain
127.0.0.1 localhost
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
Then I try to change the ip 127.0.1.1 with my server ip address and then I reinstall my ssl certificate using command:
sudo certbot --apache -d domain.com -d www.domain.com
It says ssl successfully install but when I try to access my website it is still not working and showing ssl error or when i try without ssl it says access forbidden.
What I understand from logs is that it is not searching my file directory /var/www/domain/ it is searching file directory of default configuration which I disabled /var/www/html/.
So to solve the issue temporary I enabled default host file and changed the directory from /var/www/html/ this to /var/www/domain/ and below in domain.com.conf and domain.com-le-ssl.conf host file I changed https to http
RewriteEngine on
RewriteCond %{SERVER_NAME} =domain.com [OR]
RewriteCond %{SERVER_NAME} =www.domain.com
RewriteRule ^ http://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
So website start working for http and redirecting all traffic from https to http. But I need to install ssl and want to make all configuration proper. Can anyone help me out to solve this issue!
Edit -
here is my vhost 443 entry
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/domain
ServerName domain.com
ServerAlias www.domain.com
<Directory /var/www/domain/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<IfModule mod_dir.c>
DirectoryIndex home.php index.pl index.cgi index.html index.xhtml index.htm
</IfModule>
RewriteEngine on
RewriteCond %{SERVER_NAME} =domain.com [OR]
RewriteCond %{SERVER_NAME} =www.domain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/letsfindcourse.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/letsfindcourse.com/privkey.pem
</VirtualHost>
</IfModule>
When releasing new versions of my Symfony project, I need to redirect all external ip addresses to a maintenance.html page (which lives into symfony web directory) using apache.
Here is the virtualhost file configuration I've created for this purpose but:
When I go from an external ip I get 403 Forbidden
When I go from localhost I get the same 403 Forbidden
virtualhost file
<VirtualHost *:80>
ServerName myservername
ServerAlias www.myservername
ServerAdmin webmaster#localhost
DocumentRoot /path/to/my/symfony/project/web
RewriteEngine on
# if request not coming from localhost
RewriteCond %{REMOTE_ADDR} !127.0.0.1
# if request not maintenance.html page
RewriteCond %{REMOTE_URI} !/maintenance.html [NC]
# if request not image
RewriteCond %{REMOTE_URI} !\.(jpe?g?|png|gif|ico) [NC]
# if request not css file
RewriteCond %{REMOTE_URI} !\.(css) [NC]
# Redirect to maintenance.html page
RewriteRule .* /maintenance.html [R=302,L]
<Directory /path/to/my/symfony/project/web>
# Ignore .htaccess files
AllowOverride None
</Directory>
ErrorLog ${APACHE_LOG_DIR}/maintenance-error.log
CustomLog ${APACHE_LOG_DIR}/maintenance-access.log combined
</VirtualHost>
What am I doing wrong?
Technologies versions
apache2 -v 2.4.7 (needs to work on 2.2 too)
mod_rewrite doesn’t know a %{REMOTE_URI} variable. Probably you mean %{REQUEST_URI}.
I know this is quite old problem, but i have no luck when trying to find the right solution. I have tried every possible configuration but still no luck. I'm working on PHP project using laravel. I got no problem at all when set up the laravel.labs as vhost INSIDE the htdocs. But when I move it into separated folder which is C:\vhosts i got this 403 error. The purpose of vhosts folder is to hold multiple vhost. The following are my configuration:
System:
windows 7 64-bit
XAMPP v 1.7.7
Apache/2.2.21
PHP: 5.3.8
httpd.conf :
# Virtual hosts
Include "conf/extra/httpd-vhosts.conf"
httpd-vhosts.conf
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot C:/xampp/htdocs
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot c:/vhosts/laravel_labs/public
ServerName laravel.labs
</VirtualHost>
c:\windows\system32\driver\etc\host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 laravel.labs
.htaccess (inside the C:\vhosts\laravel_labs\public)
<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>
Can anyone help to find something that i missed? any advice guys? thanks
Every time you add a new Alias or DocumentRoot, you need a <directory> section just like the one for your default DocumentRoot. Depending on your release, this either has something like
Order deny,allow
or
require all granted
Finally i found The answer for this problem. I have to add some lines in httpd.conf
Something like this:
Alias /laravel_labs/public "C:/vhosts/laravel_labs/public/"
<Directory "C:/vhosts/laravel_labs/public/">
AllowOverride All
Order Allow,deny
Allow from all
</Directory>
See the detail explanation here :
http://www.youtube.com/watch?v=Z250saioXIs
I'm trying to redirect all my foo.mydomain.com requests to https://foo.mydomain.com.
My VirtualHost contains this:
<VirtualHost *:443>
ServerName foo.mydomain.com
# Indexes + root dir
DirectoryIndex index.html
DocumentRoot /home/web/foo.mydomain.com/htdocs
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/server.crt
SSLCertificateKeyFile /etc/apache2/ssl/server.key
SSLVerifyClient none
<IfModule mime.c>
AddType application/x-x509-ca-cert .crt
AddType application/x-pkcs7-crl .crl
</IfModule>
# Logfiles
ErrorLog /home/web/foo.mydomain.com/logs/error.log
CustomLog /home/web/foo.mydomain.com/logs/access.log combined
</VirtualHost>
So now I'm trying to redirect all the http-request to https with mod_rewrite in this .htaccess file:
Options FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://foo.mydomain.com
Does anyone knows what I'm doing wrong? Thank you in advance!
Try the following in virtual host config for the non https site.
I.e. my settings to do this are:
<VirtualHost *:80>
RewriteCond %{SEVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
.....
</VirtualHost>
This should redirect all incoming http request to https. Then in the config for port 443 you can do whatever you need....
Note: should work in htaccess as well.
Hope that helps. Not sure what you meant by 'default page' but in title and too low rep to leave comments...
Let me know...
Cheers
Robin