Apache2 + Laravel wildcard subdomain routing - laravel

I'm building webapp on Ubuntu + Apache2 with wildcard SSL certificates
My goal is to set up functioning project on 1st level domain project.app as well as several subdomains marketplace.project.app & partners.project.app
All of the project files are under the same directory:
www/project/
subdomains accessible via laravel router:
Route::domain('marketplace.project.app')->group(function () {
Route::view('','pages.marketplace');
});
serverside DNS records:
A | project.app | 127.0.0.1
A | *.project.app | 127.0.0.1
/sites-available/default-ssl.conf:
ServerName project.app
ServerAlias *.project.app
DocumentRoot /var/www/project/public
<Directory /var/www/project>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^127\.0\.0\.1
RewriteRule (.*) https://project.app/$1 [R=301,L]
All of the wildacrd subdomains e.g. bbb.project.app work flawlessly, but upon accessing app in browser via 1st level project.app apache does not render index.php file.
Is the setup I'm thriving for feasible in apache or should I switch to Ngnix?

Just to alert, You do not really need to use htaccess rules once you use apache2, ybecause "blahblahblah.conf" can handle htaccess rules.
To handle multiple domains and including htaccess rules with it, you can do the following (working example):
<VirtualHost *:80>
ServerName example.com
ServerAlias foo.example.com bar.example.com others.example.com
DocumentRoot "/var/www/laravel_folder/public"
<Directory /var/www/laravel_folder/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com [or]
RewriteCond %{SERVER_NAME} =www.example.com [or]
RewriteCond %{SERVER_NAME} =foo.example.com [or]
RewriteCond %{SERVER_NAME} =www.foo.example.com [or]
RewriteCond %{SERVER_NAME} =bar.example.com [or]
RewriteCond %{SERVER_NAME} =www.bar.example.com [or]
RewriteCond %{SERVER_NAME} =others.example.com [or]
RewriteCond %{SERVER_NAME} =www.others.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

Related

Laravel subdomain on virtual host setup on ubuntu

I am trying to set Larval up on a virtual host as a sub domain. when I go to the site it shows the index directory. in my /etc/apache2/sites-available/.conf file, my document root is pointing to public. when I click on the public folder in the browser the server gives a 500 error.
<VirtualHost *:80>
ServerName subdomain.mainwebsite.space
ServerAlias www.subdomain.mainwebsite.space
ServerAdmin support#mainwebsite.com
DocumentRoot /var/www/subdomain/public
<Directory /var/www/subdomain/public>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
#Redirect "https://subdomain.mainwebsite.space"
#SSLEngine on
#SSLCertificateFile /etc/encryption/certificate.crt
#SSLCertificateKeyFile /etc/encryption/server.key
#SSLCertificateChainFile /etc/encryption/intermediate.crt
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
#RewriteCond %{SERVER_NAME} =subdomain.mainwebsite.space [OR]
#RewriteCond %{SERVER_NAME} =www.subdomain.mainwebsite.space
#RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
<VirtualHost/>
I have also changed permissions on public folder to 775. I am not sure what else i should be doing. I have a static public ip and a static internal ip and restarted apache service. Any help will surely be appreciate

Laravel - set /public as root on LAMP stack

I'm trying to deploy a Laravel App on a LAMP stack although I am having trouble setting the document root as /public. I've looked at several answers on here already for similar problems but am yet to find my problem or a solution. I ideally I want to do it through the Apache web server and not modify any Laravel files.
I copied the default .conf file a change it to the following using sudo nano /etc/apache2/sites-available/subdomain.website.com.conf
<VirtualHost *:80>
ServerAdmin email#gmail.com
ServerName subdomain.website.com
ServerAlias www.subdomain.website.com
DocumentRoot /var/www/subdomain.website.com/public_html/public
<Directory /var/www/subdomain.website.com/public_html/public/>
Options Indexes FollowSymLinks MultiViews
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>
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.subdomain.website.com [OR]
RewriteCond %{SERVER_NAME} =subdomain.website.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
After the I ran the following commands in this order.
sudo a2ensite subdomain.website.com.conf
sudo a2enmod rewrite
sudo systemctl restart apache2
sudo service apache2 restart
I cleared cookies, did a hard refresh and it hasn't worked. I still have to go to https://subdomain.website.com/public to view the website. Is there anything I have missed or anything I can do find out what the problem may be?
The .htaccess file in the /public folder contains the following
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
This virtualhost is for http:// (Port 80). You have a rewrite rule that redirects to https:// as soon as it hits this virtualhost.
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.subdomain.website.com [OR]
RewriteCond %{SERVER_NAME} =subdomain.website.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
Therefore, you need to apply your changes to the other virtualhost that is serving this domain from https:// (Port 443).

URL rewriting in order to use joomla SEF

I'm very new to apache. I read and read docs, turn all conf files I found, but ...
While I'm root on the webserver, I read that in order to optimize apache2, it is not a good idea to use .htaccess files (http://httpd.apache.org/docs/current/howto/htaccess.html).
OK, I read a lot of howtos about url_rewriting and htacess. I tried this code into my www.site.com virtualhost file :
<VirtualHost *:80>
ServerName site.com
ServerAlias www.site.com
ServerAdmin webmaster#site.com
DocumentRoot /var/www/www.site.com
RewriteEngine On
<directory />
RewriteBase /var/www/www.site.com/
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Please note that RewriteBase has been tested with DocumentRoot value and '/', and that all the RewriteCond come form std_htaccess of latest Joomla! source code.
Apache2 launches well, the site is viewed online with no error, but the rewrite is not working as joomla SEF is not working :(
I'm totaly lost ... again ... and I need your help guys !
Ok, the correct way is
<directory /var/www/www.site.com/>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw))$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
Options FollowSymLinks
AllowOverride None
</Directory>

how to properly mod_rewrite

I am using apache2 + tomcat on ubuntu 10.4. I am running on tomcat server that has webapps listening on 8080
"http://internal:8080/dir/name-privatewebapp/"
"http://internal:8080/dir/name-publicwebapp/"
External facing Apache server is proxying requests for the domain.
I would like to remap "subdomain" to "domain.com/subdomain" , so any other requests would be proxied to the appropriate path / webapp
web.john-doe.domain.com ---> www.domain.com/web/john-doe
http://www.john-doe.com ---> www.domain.com/web/john-doe
http://www.foo-bar.com ---> www.domain.com/web/foo-bar
I couldnt make it work with mod_rewrite alone. I have been looking into
mod_proxy, mod_proxy_ajp, and mod_rewrite
I would like the server to
Grab the subdomain from the domain
Make sure the subdomain is not www, w or ww
Check if the directory actually exists on "www.domain.com" before rewrite
if the directory doesnt exist it stays as wild domain
Finally, if the directory exist the actual rewrite
I checked several links and studied tutorials on http://www.askapache.com/htaccess/crazy-advanced-mod_rewrite-tutorial.html
It is without success.
I followed this
illumin-it.servehttp.com/wordpress/2012/01/redirecting-to-as-using-apache/
with a bit of success.
However, I would like to the server to do it on the fly through rewrites and proxies without editing the vhost for every subdomain.
here is my updated vhost file
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName www.domain.com
ServerAlias www.domain.com domain.com *.domain.com
RedirectPermanent / http://www.domain.com
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
# Prevent looping this rule
# RewriteCond %{ENV:REDIRECT_STATUS} !200
# RewriteCond %{HTTP_HOST} !www.domain.com$ [NC]
# RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9.-]+).domain.com$ [NC]
# RewriteRule (.*) /%2/$1 [L]
# change the "." in the path to "/"
# RewriteRule ^(.*)\.(.*)/(.*)$ /$1/$2/$3 [L]
#redirect domain.com to www.domain.com
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteRule ^ http://www.domain.com%{REQUEST_URI} [R=301,NC]
</IfModule>
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
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
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
# ErrorLog /var/log/apache2/webapp_error.log
# LogLevel warn
# CustomLog /var/log/apache2/webapp_access.log combined
ProxyPass / ajp://localhost:8009/
ProxyPassReverse / ajp://localhost:8009/
# ProxyPass /user/ http://localhost:8080/user/privatewebapp/
# ProxyPassReverse /user/ http://localhost:8080/user/privatewebapp/
#
# ProxyPass /publicweb/ http://localhost:8080/dir/publicwebapp/
# ProxyPassReverse /publicweb/ http://localhost:8080/dir/publicwebapp/
#
# ProxyPass / http://localhost:8080/dir/publicwebapp/
# ProxyPassReverse / http://localhost:8080/dir/publicwebapp/
ServerName www.domain2.com
ProxyRequests Off
ProxyPreserveHost On
RewriteEngine On
RewriteCond %{HTTP_HOST} ^dir/publicwebapp$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [R=301,L]
<Location / >
Order deny,allow
Allow from all
RedirectMatch ^/$ /dir/publicwebapp/
ProxyPass ajp://localhost:8009/
ProxyPassReverse ajp://localhost:8009/
</Location>
</VirtualHost>
I also tried...
http://httpd.apache.org/docs/2.0/rewrite/rewrite_guide.html
assuming www.username.host.com/anypath internally maps to www.host.com/home/username/anypath
As the site's applications created with Java servers, I believe mod_rewrite alone doesnt work.
Virtual User Hosts
RewriteCond %{HTTP_HOST} ^www\.[^.]+\.host\.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^www\.([^.]+)\.host\.com(.*) /home/$1$2
Your first set of rules:
#redirect domain.com to www.domain.com
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule .+ http://www.domain.com%{REQUEST_URI} [R=301,NC]
Is superceding any subdomain you have if you don't have an empty request. The second set of rules comes close to what you want. But they loop and they don't turn multiple subdomain entries into directory paths, so try putting this set of rules first:
# Prevent looping this rule
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{HTTP_HOST} !www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9.-]+).domain.com$ [NC]
RewriteRule (.*) /%2/$1 [L]
# change the "." in the path to "/"
RewriteRule ^(.*)\.(.*)/(.*)$ /$1/$2/$3 [L]
Then change your "non-www to www" rule to this (and place after the above rules):
#redirect domain.com to www.domain.com
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteRule ^ http://www.domain.com%{REQUEST_URI} [R=301,NC]
In similar cases I usualy put somethin like this to .htaccess in webroot (/var/www/.htaccess):
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} subdomain1.domain.com
RewriteCond %{REQUEST_URI} !^/subdomain1
RewriteRule ^(.*)$ one/$1 [L]
RewriteCond %{HTTP_HOST} subdomain2.domain.com
RewriteCond %{REQUEST_URI} !^/subdomain2
RewriteRule ^(.*)$ two/$1 [L]
RewriteRule ^$ / [L]
RewriteRule (.*) /$1 [L]
</IfModule>
so I have
subdomain1.servername.com -> /var/www/subdomain1
subdomain2.servername.com -> /var/www/subdomain2
servername.com -> /var/www
Maybe this will help you?

vHost RewriteRule is creating a 500 Error

Below you will find my current vHost entry that I am using for a site that I currently have under development. This vHost entry works fine when I have it on my local machine, but when I push my code to my staging server that is running this same vHost record I receive a 500 Internal Server error.
The machine I'm running this vHost on is running Apache 2.2.9 (Debian).
<VirtualHost 206.217.196.61:80>
SuExecUserGroup 13labs 13labs
ServerAdmin aellis#1three.com
ServerName admin.13labs.net
ServerAlias admin.13labs.net
DirectoryIndex index.php
DocumentRoot /var/www/13labs.net/html/admin/
ErrorLog /var/www/13labs.net/logs/error.log
# Hide .svn Directories
<DirectoryMatch "\.svn">
Order deny,allow
deny from all
</DirectoryMatch>
# FastCGI
Alias /fcgi-bin/ /var/www/13labs.net/fcgi-bin/
AddHandler php-fastcgi .php
AddType application/x-httpd-php .php
Action php-fastcgi /fcgi-bin/admin-php.fcgi
<Directory /var/www/13labs.net/fcgi-bin/>
SetHandler fcgid-script
AllowOverride None
Options -Indexes +ExecCGI -FollowSymlinks -SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
<Directory /var/www/13labs.net/html/admin/>
AllowOverride None
Options -Indexes -FollowSymlinks -SymLinksIfOwnerMatch
FileETag All
</Directory>
# Rewrite Logic
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/(.+)$ /index.php/$1 [PT,QSA,L]
Thanks for any help that you can provide.
Best regards,
Andrew
After many trial and errors I have found that the working RewriteRule needs to be the following:
RewriteRule ^.*$ /index.php$1 [PT,QSA,L]
Then in PHP I need to be using $_SERVER['REQUEST_URI'] instead of $_SERVER['PATH_INFO'] to make sure my PHP script see the passed in URI.

Resources