400 Bad Request Error while creating a virtual domain in Ubuntu
Problem Statement: I have more than 3 PHP projects in the workspace which is at path /var/www/html in Ubuntu 14.04 system. When I open my browser and type http://localhost, then the list of all PHP, CakePHP, or laravel projects is shown on screen. Suppose I have a project “laravel_proj” in my workspace, then the URL in the browser is shown as “ http://localhost/laravel_proj".
I want the URL to be www.laravel_proj.com, which means I want to create a virtual domain for laravel_proj, and the rest projects can be opened as usual as they open through localhost.
Let’s see how I created a virtual domain for my laravel_proj and what issue I faced while creating the virtual domain and then I will share how I resolved the problem I faced.
Creation of Virtual Domain
- I typed following command in terminal:
sudo gedit /etc/apache2/sites-available/000-default.conf
with above command 000-default.conf file will be opened in textfile form. - I found the following lines of code in 000-default.conf file
<VirtualHost *:80>
# Lot of commented lines
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
With the above lines of code, each project opens withhttp://loclahost on the browser. - I added the following lines of code after the above lines of code for creating a virtual domain for my project laravel_proj
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/laravel_proj
ServerName www.laravel_proj.com
ServerAlias laravel_proj.com
<Directory /var/www/html/laravel_proj>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost> - Now I typed the following command in the ubuntu terminal:
sudo gedit /etc/hosts
this file is also opened as a textfile to modify or add changes. The following line was already written in this file:127.0.0.1 localhost
- I added a new line in the hosts file opened in the above file:
127.0.0.1 laravel_project.com
- After making above changes, I restarted the apache2 with following command:
sudo /etc/init.d/apache2 restart
So, by following the above steps, I created a virtual domain for my projectlaravel_proj
The issue I faced: 400 Bad Request
When I opened the browser and typed “ laravel_project.com”, then “400 Bad Request” was displayed for the opened URL as shown below:
Solution:
I searched from google regarding “400 Bad Request error reasons” for general-purpose, not for PHP projects or Ubuntu systems. Somewhere I found the line “underscore (_) is not allowed in URL”.
Then I removed underscore from the virtual domain I created and the new URL becomes “ laravelproj.com” and I changed domain in both the files
/etc/apache2/sites-available/000-default.conf and /etc/hosts through terminal commands and run the apache restart command.
When I opened the URL “ laravelproj.com “ on the browser, everything was working perfectly fine.
So, this is the one reason that I came to know that underscore is not allowed in domain names and that may cause a Bad Request Error.
So, in this sheet, you now aware that how virtual domains can be easily created in Ubuntu. I have mentioned one reason here for Bad Request error and if you face a bad request error due to any of the reasons, you may search what reasons may cause Bad request error and modify the domain URL accordingly.
Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.
Originally published at https://blog.webnersolutions.com on January 4, 2021.