Subdomain redirects to the main domain (Apache2) - php

I asked this question to DigitalOcean questions section but did not get an answer, so I am trying my luck with you guys. Here it goes:
I know this question has been asked at least a million times, but I tried all the solutions or recommendations that I could find and it still doesn't work.
The droplet is using Ubuntu 16.04, I am trying to setup sub domain with a virtual host in Apache2.
At first, I followed this DigitalOcean tutorial How To Set Up Apache Virtual Hosts on Ubuntu 16.04.
On top of that, I created A record in domain panel with a hostname of sub.example.com pointing to my droplet.
I noticed when I type sub.example.com shows my main domain, which is on main domain or exactly running on Nginx. URL in URL bar stays sub.example.com
I checked my /etc/hosts file and added an entry like this (last one):
127.0.1.1 blog blog
127.0.0.1 localhost
123.345.554.21 www.sub.example.com sub.example.com
My virtual host file looks like this:
ServerAdmin admin#example.com
ServerName sub.example.com
ServerAlias www.sub.example.com
DocumentRoot /var/www/sub.example.com/public_html/
<Directory /var/www/sub.example.com/public_html>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
I used a2ensite command to enable the site. I tried using the command with .conf extension and without. I disabled default .conf file (000-default or something like that).
I tried adding .htaccess file in my project directory with this in it:
RewriteEngine on
# redirect to https www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)(domain\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]
# redirect to http subdomain
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^((?!www).+\.sub.domain\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
Paths to the document root are correct and double-checked, and even if I change to incorrect route, nothing happens. I restarted apache2 service and droplet more times than I can count. I added permissions to the directory.
I don't know what should I do anymore. Maybe I am missing something? Is it maybe because my index file is a .php file?
Anyways, thank you for your help!

Related

Apache virtual host always redirecting to /dashboard

I'm having what appears to be a common problem but any solutions I've found don't seem to work for my case.
I'm trying to set up a virtual host so that I can access the public file of my Laravel installation by going to "mytestdomain.local" but when I type this address into google chrome I am always redirected to the xampp dashboard at this address "https://mytestdomain.local/dashboard/".
I've installed Laravel in the following xampp directory: c:/xampp/htdocs/mytestdomain_uk.
I have "C:\xampp\apache\conf\extra\httpd-vhosts.conf" set up as follows:
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/mytestdomain_uk/public"
ServerName mytestdomain.local
</VirtualHost>
And I have "C:\Windows\System32\drivers\etc\hosts" set up as follows:
127.0.0.1 localhost
127.0.0.1 mytestdomain.local
If anyone could offer any insight into this issue I would be very grateful.
Put this as the first line in C:\...\httpd-vhosts.conf (and restart the web server):
NameVirtualHost *:80
So, it should look like this:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName localhost
DocumentRoot "C:/xampp/htdocs"
</VirtualHost>
<VirtualHost *:80>
ServerName walkpeakdistrict.local
DocumentRoot "C:/xampp/htdocs/walkpeakdistrict_uk/public"
</VirtualHost>
I would place all my projects somewhere outside of C:/xampp/htdocs and C:/xampp. Let C:/xampp/htdocs be the standard localhost location, with just two files inside (simple index.php and index.html), but use another one for the projects. Even better, use another partition, not the system partition C:. Like D:/projects, or so. So, you would have D:/projects/walkpeakdistrict_uk.
Good luck.
Ok, I'm not sure why this was an issue but it seems to work when I change the virtual host's server name to anything other than ".local".
Thanks again to all who replied!
This happened with me as well. And I resolved it. In my case, it was happening because of the SSL module. So, turning it off got me out of the trouble. Just Open the httpd.conf file and comment the SSL include code by adding # to the front of the code as given below.
# Secure (SSL/TLS) connections
# Include conf/extra/httpd-ssl.conf
After this, it will be resolved.
I'm pretty sure the issue for me had to do with SSL redirects, for some reason. When I edited my .htaccess to exclude local.mydomain.com when forcing https, I stopped getting redirected to the XAMPP dashboard.
Below is the section of my .htaccess that excludes local sites from redirecting to https. You can add extra RewriteCond lines for other local domains.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTP_HOST} !local\.
RewriteCond %{HTTP_HOST} !other.localdomain.example.com
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]
</IfModule>
<IfModule mod_headers.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !local\.
RewriteCond %{HTTP_HOST} !other.localdomain.example.com
Header set Strict-Transport-Security "max-age=16070400" env=HTTPS
</IfModule>
Change your document root to this, just add a slash at the end of public and it will work
DocumentRoot "C:/xampp/htdocs/walkpeakdistrict_uk/public/"
I had this problem after developing my website and I got round it by simply rebooting my machine. Many months later, Google then invalidated the use of .dev so my virtual host stopped working. I changed the name of my virtual host to .test and got the dashboard. I had forgotten the simple solution and arrived on this page searching for a solution. Then i remembered......I rebooted my machine and hey presto....it worked. No more dashboard. I get the right homepage.
In your case, you put localhost settings in virtual host. check if you got this script on default file conf, then comment it. Case on xampp for mac
Include "${your_xampp_path}/conf/httpd.conf"
The solution is very simple, it always redirects the https url to the Dasboard, you just have to remove the s leaving http:
Example:
http://juego123.com
I was facing the same error. I solved the error by removing the *80 port number from the virtual host tag and then my code look like
<VirtualHost *>
ServerName localhost
DocumentRoot "C:/xampp/htdocs"
</VirtualHost>
<VirtualHost *>
ServerName walkpeakdistrict.local
DocumentRoot "C:/xampp/htdocs/walkpeakdistrict_uk/public"
</VirtualHost>
**NOTE:**Your Virtual Host tag looks like this <VirtualHost *>
This means we are redirecting to any other port or not bounding the virtual host to port 80 this will work definitely.
I came here with the same problem, in my case accessing the website through http instead https worked.
In browser, instead of accessing this way:
https://mytestdomain.loc
try this one
http://mytestdomain.loc
Its works for me once I remove header location from 'htdocs/index.php' file

How to perfectly set up virtual host for codeigniter project?

I am trying to make a virtual host for a codeigniter project. I have done this in httpd-vhosts.conf:
<VirtualHost *:80>
DocumentRoot "C:\xampp\htdocs\CI_projects\facebook-login"
ServerName dev.facebook-login.com
<Directory "C:\xampp\htdocs\CI_projects\facebook-login">
Require all granted
</Directory>
</VirtualHost>
and in application/config/config.php,
$config['base_url'] = 'http://dev.facebook-login.com';
and
$config['index_page'] = '';
the browser opens the landing page. but when transiting from any other uri it says object not found.
And when i configure httpd-vhosts.conf like this:
<VirtualHost *:80>
DocumentRoot "C:\xampp\htdocs\CI_projects\facebook-login\index.php"
ServerName dev.facebook-login.com
<Directory "C:\xampp\htdocs\CI_projects\facebook-login\index.php">
Require all granted
</Directory>
</VirtualHost>
It arises problem with assets things, i,e images and some css doesnt loads. How can I solve it?
I am on windows 10 and I use xampp with virtual host this is way I set up.
Put forward slash at end of base url
$config['base_url'] = 'http://dev.facebook-login.com/';
First go to
Windows > System32 > drivers > etc > host
You may need to open as administrator to be able to save it
You might see some thing like
127.0.0.1 localhost
Below that create another one for dev.facebook-login.com like example:
127.0.0.1 localhost
127.0.0.1 dev.facebook-login.com
Save it
Then go to the
xampp > apache > conf > extra > httpd-vhosts.conf
open it as administrator so you can save it.
No need for the index.php on DocumentRoot
<VirtualHost *:80>
##ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "C:/xampp/htdocs/CI_projects/facebook-login"
ServerName dev.facebook-login.com
##ServerAlias www.dummy-host.example.com
##ErrorLog "logs/dummy-host.example.com-error.log"
##CustomLog "logs/dummy-host.example.com-access.log" common
</VirtualHost>
Save it and restart the severs.
I use this for my htaccess
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
httpd-vhosts.conf
Listen 1122
<VirtualHost *:1122>
DocumentRoot "D:\laragon_server\www\hugpong_production"
<Directory "D:\laragon_server\www\hugpong_production">
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
https://www.namecheap.com/support/knowledgebase/article.aspx/10026/33/installing-an-ssl-certificate-on-xampp/
php 8 is not ready for CI4 as of jan 2021!
XAMPP has preloaded ssl certificate in php 7 packages
consider uncommon vhosts extra and place this in it
my only persistent issues is codeigniter 4 routes, have to turn on php spark serve to get XAMPP to route pages outside of default index, nothing else works, its garbage to that extent, will try with composer next....a whole book needs
to be wrote about codeigniter 4 routes and why they don't work, 200 pages plus
<VirtualHost localhost *:80>
DocumentRoot "C:\xampp\htdocs"
ServerName localhost
<VirtualHost example.com *:8080>
ServerName example.com
ServerAlias www.example.com
DocumentRoot "C:\xampp\htdocs\codeignite4\public"
//my project folder is codeignite4 without using composer this time
//i added listen 8080 under listen 80 in htppd.conf
ServerName example.com
<VirtualHost truservex.com *:443>
DocumentRoot "C:\xampp\htdocs\codeignite4\public"
ServerName example.com
SSLEngine On
SSLCertificateFile "C:/xampp/apache/conf/ssl.crt/server.crt"
// you can verifiy these file locations, certificate data
SSLCertificateKeyFile "C:/xampp/apache/conf/ssl.key/server.key"
// going without the Directory tag can be helpful to drop in vhosts
<Directory "C:\xampp\htdocs\codeignite4\public">
Require all granted
doing this i was able to replicate the original install, create site access
using http://localhost:8080 or http://example.com/ as my base url
in my example index.php is removed, set to ''
when using command prompt to get into codeignite4 project folder i can
launch php spark serve and routes will work that were previously 404
Using Xampp 7.3.25 Dec 2020 because php 8 will not run error
free with codeigniter 4 as of this date, this worked for me as
my first codeigniter 4 virtual host setup, my install had
difficulty initially with using port 80, seems to want to for
port 443 if it can..I'm not a server pro but i wanted to
leave this for the next guy....This is strictly name based
virtual hosts for develop, ip based would have obvious
advantages. Because i had trouble in initial CI4 testing with
port 80 the (*) in virtual hosts vs (*:80) seemed to solve one
hurdle in searching for more ports, now i can finally begin my
first project ...my default URL was also changed to example.com
in appstarter/apps/config file. Carefully reread <tags> for
typos, its easy to shut server down!
<VirtualHost *>
DocumentRoot "C:/Xampp/htdocs/"
ServerName localhost
<Directory "C:/Xampp/htdocs/">
Require all granted
<VirtualHost *>
DocumentRoot "C:/Xampp/htdocs/appstarter/public/"
ServerName example.com
ServerAlias www.example.com
# maybe don't attempt to use dir tag, shuts down server
# the directives at httpd.conf appear to suffice
I have added a htaccess file in the appstarter/public
I assume this is primitive, can get far more advanced
The Apache help pages helped me focus a bit more on
what i must accomplish, name based virtual hosts this case
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
2.2 Apache configuration: :Apache server context important!
Order allow,deny :The hackers are winning.....
Allow from all
2.4 Apache configuration:
Require all granted

URL Rewriting and Laravel 4 Routes not working on server?

I bet this question has been asked time and time again, but I just can't seem to get an answer. Anyway, here is my situation. I have a domain, lets say example.com. Example.com works fine, everything works as it should. However, if I navigate to Example.com/about, which works on my local machine, I get a 404 error. However, if I go to example.com/index.php/about, the about page works fine. I know this must be a mod rewrite issue. I have enabled mod rewrite and restarted my server to no avail. Anyway, here is some code:
Virtual Host:
<VirtualHost *:80>
ServerName example.com
DocumentRoot "/var/www/example/public"
<Directory "/var/www/example/public">
AllowOverride all
Allow from All
</Directory>
</VirtualHost>
And here is my .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>
I'm using Ubuntu 14.04 on a Digital Ocean Droplet.
Thanks!
1)
go to /etc/apache2/sites-available and create file and add below
<VirtualHost *:80>
ServerName example.com
ServerAdmin webmaster#newyear
DocumentRoot /var/www/html/example/public
<Directory /var/www/html/example/public>
options Indexes FollowSymlinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
2) Create Link of above created file and paste it into /etc/apache2/sites-enabled
3) Check /etc/apache2/mods-available for rewrite.load file
if it is not there create it and add below line & save
LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so
create link of rewrite.load file and paste it into etc/apache2/mods-enabled
4) open /etc/hosts
add below
127.0.0.1 example.com
5) sudo service apache2 restart
I figured out how to do this, I had to put my VirtualHost code in sites-enabled, not sites-available. Facepalm.
In ubuntu 14.04 with apache2 i was fall this problem.Then i solve this and wrote a blog,you can follow this
http://www.kingpabel.com/apache-mod_rewrite/

Redirect with www not working

I recently switched from a shared hosting to a VPS.
Everything is working fine and I can access al of my domains.
But I seem to have 1 problem:
In the past all non-www url's where redirected to url's with www.
I did this with the following htaccess.txt code:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.tx3\.be$ [NC]
RewriteRule ^(.*)$ http://www.tx3.be/$1 [R=301,L]
But I'm using a new htaccess at the time with the following code:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^off(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
But both are not working on redirecting my non-www domain.
When I visit my non-www domain, I get the following page:
"Apache is functioning normally"
I tried using the DirectAdmin redirect for redirecting the non-www domain to a domain with www, but that also didn't work.
Maybe I've got it all wrong and it wasn't a htaccess problem in the first place.
Did I forget something in my server settings?
I don't know, everything looks to be in order.
(I did double check my DNS settings and they appear to be fine)
Does anyone know what I should be doing next?
Please try this
Rewriteengine on
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
If using windows download putty (ssh client) give it your servers ip address select type as ssh and click open.
It'll ask you for a username and password give it your root username and password (should have been provided by vps hosting company)
Now cd through to your apache config folder (I'm only familiar with debian based setups if you're using a redhat based setup you may have to google correct path) /etc/apache2/sites-available
Now look at the directory listings (ls) and it'll show you the apache configs available chances are you'll have something like www.tx3.be in there. Edit that file I recommend using pico or nano by doing pico
Here's an example off my server
root#hinch:/home/hinch# cd /etc/apache2/sites-available
root#hinch:/etc/apache2/sites-available# ls
default
default-ssl
root#hinch:/etc/apache2/sites-available# pico default
Now you're in your apache config file you'll see something like this
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin root#yourdomain.com
DocumentRoot /var/www/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm default.htm default.html default.php
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/access.log combined
</VirtualHost>
After the line that says ServerAdmin add in the following
ServerAlias tx3.be
ServerAlias www.tx3.be
Save the file and restart apache
service apache2 restart or service httpd restart
Delete all redirect code from your .htaccess and make sure you have both the www. dns A record and the #. A record pointing to your servers ip address. Now regardless of which version of the domain the user goes to they'll see your site.

Laravel 4 Virtual Host and mod rewrite setup

I've been trying for a few hours to install Laravel 4 and make the Virtual Hosts and Routing work but so far I've been unlucky. I'm mentioning that I'm doing this on a Windows 7 machine and WAMP.
I'll describe what I've already done:
I've enabled rewrite_module.
Updated httpd-vhosts.conf with the following:
<VirtualHost *:80>
DocumentRoot "c:/wamp/www/laravel/public"
ServerName laravel.dev
ServerAlias www.laravel.dev
</VirtualHost>
Next, I've set up the etc/hosts:
127.0.0.1 localhost
127.0.0.1 laravel.dev
Also, the following lines are uncommented both in wamp/../conf/httpd.conf and in wamp/.../conf/original/httpd.conf:
LoadModule vhost_alias_module modules/mod_vhost_alias.so
Include conf/extra/httpd-vhosts.conf
The content of my .htaccess is the following:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Yet, when I go to http://laravel.dev I get an Internal Server Error - The server encountered an internal error or misconfiguration and was unable to complete your request..
If i remove the .htaccess file from the public folder, then I'll see the Laravel "You have arrived" message. If then, i create a route like this (within the app\routes.php):
Route::get('/newroute', function()
{
return View::make('hello');
});
and then go to http://laravel.dev/newroute I'll end up with a 404 Not Found - The requested URL /newroute was not found on this server.
I am seriously confused and have no idea how I can get past this. I mention (although i think it's clear) that I ran a composer install after cloning the project template, installing the dependencies.
If the htaccess file that you posted was in your /public/ folder, then that's why you're getting a 500 internal server error. That htaccess file was made for the folder that the /public/ folder is in. But since your public folder is the document root, you don't need it.
The htaccess file in the /public/ folder should look like this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
(for WAMP).
Try turning on AllowOverride All on the directory by changing your VirtualHost to:
<VirtualHost *:80>
DocumentRoot "c:/wamp/www/laravel/public"
ServerName laravel.dev
ServerAlias www.laravel.dev
<Directory "c:/wamp/www/laravel/public">
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
I have gone through the same issue and followed the all the treatments given above but virtual host can not be set.
When I inspected file http.conf then I found that inclusion of file httpd-vhosts.conf was commented as given below. Make sure to include httpd-vhosts.conf in httpd.conf.
#Include conf/extra/httpd-vhosts.conf
I have removed the # comment. Restarted the wamp and it was working.
Include conf/extra/httpd-vhosts.conf

Categories