Setup subdirectory redirect to a Laravel project - apache

I have three projects, First one is a static html website and other 2 projects are Laravel projects.
I have an SSL certificate set up for the main website. I want to setup the other Laravel projects as subdirectories of the main URL.
I added an alias to my conf file, but it doesn't work. I get this error: "Too many redirects occurred trying to open “https://example.com/admin/”. This might occur if you open a page that is redirected to open another page which then is redirected to open the original page."
Are there any changes that I should add to htaccess file?
Here is my website.conf file
<VirtualHost *:80>
ServerName example.com
Redirect permanent / https://example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/***********.crt
SSLCertificateKeyFile /etc/apache2/ssl/*********.key
SSLCertificateChainFile /etc/apache2/ssl/*********.crt
ServerAdmin admin#example.com
DocumentRoot /var/www/html/example/public_html/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /admin "/var/www/html/admin"
<Directory "/var/www/html/admin">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Alias /admin2 "/var/www/html/admin2"
<Directory "/var/www/html/admin2">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>

I would suggest taking a look at Vagrant with Laravel Homestead prebuilt box. This will achieve what you want to do by editing the Homestead.yaml file, the /etc/hosts file, and then provisioning the vagrant machine. You will need virtual box installed to work with vagrant. You can SSH into the machine by typing vagrant ssh inside the homestead directory.
Documentation: https://laravel.com/docs/5.4/homestead
YouTube Video about Vagrant: https://www.youtube.com/watch?v=LyIyyFDgO4o
Youtube Videos about Installing Laravel Homestead: https://www.youtube.com/watch?v=tbPGuikTzKk&list=PLfdtiltiRHWEaImCreOC7UkK4U5Xv9LdI

You should use the public folder as your alias in the apache setup.
Try "/var/www/html/admin2/public" and also for the Directory as well.
Something like this:
Alias /admin2 "/var/www/html/admin2/public"
<Directory "/var/www/html/admin2/public">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Depends on the installation of Laravel, you should have an .htaccess file in the public directory, which might require some additional tweeking.
Also, restart apache once you have made the change.t

You need to correctly set 2 things :
Your sites's alias in WAMP's alias folder e.g. C:\wamp\alias to redirect URL to public\index.php in Laravel's folder
Rewrite base .htaccess in your Laravel's public folder so that Apache rewrites on top of a base path.
Assuming that your site's directory is in C:\wamp\www\site and your alias is localhost/alias
Setting alias to point to public folder:
Alias /site "/var/www/site/public"
<Directory "/var/www/site/public">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Add RewriteBase in .htaccess in Laravel's public folder
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteBase '/site/'
# 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>

Related

How to fix " Not found " when refresh page with vue js SPA in Apache Server?

I just deploy my vue js project to apache server. My file /dist located in /var/www/html/dist. When i visit the page it's work fine. But when i visit in another page and i refresh the page, in browser say 404 Not found. How can i fix this ?
You need a configuration something similar to this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
It will automatically serve index.html file for every request that doesn't have a corresponding static file. You will have to put this .htaccess file. The use of IfModule is explained here.
I have same error, on ubuntu after i install apache.
First i added in /etc/apache2/apache2.conf
AllowOverride All
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
In /etc/apache2/sites-available
<VirtualHost test.ru:80>
ServerName www.test.ru
ServerAlias test.ru
ServerAdmin webmaster#localhost
DocumentRoot /var/www/test.ru
<Directory /var/www/test.ru>
#Разрешение на перезапись всех директив при помощи .htaccess
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Then in console activate rewrite module and restart apache
sudo a2enmod rewrite
service apache2 restart

How to set default document root for if virtual document root doesnt exist?

I have set up virtual document root using vhost_alias and it works good if directory of subdomain exists. but it throws 403 error if subdomain dir doesnt not exist. I want to set a default dir if sub domain doesn't exist.
current vhost conf
<VirtualHost *:80>
ServerAlias *.domain.com
VirtualDocumentRoot /home/ubuntu/www/domain/%1
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory "home/ubuntu/www/domain/*">
Options Indexes FollowSymLinks
AllowOverride All
Order Allow,Deny
Allow from all
Require all granted
</Directory>
</VirtualHost>
I tried using error document
ErrorDocument 403 http://app.domain.com%{REQUEST_URI}?%{QUERY_STRING}
but it changes browser url. Is there any way to redirect internally?
i have also tried rewrite but that doesn't seems to work.
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT} !-d
RewriteRule %{DOCUMENT_ROOT} "/home/ubuntu/www/domain/app" [QSA,L]
tried this but not working
Found solution
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT} !-d
RewriteRule (.*) http://app.domain.com/%{REQUEST_URI} [P,L]
Adding this inside Directory directive solved my problem. For SSL sites include this in Virutalhost block
SSLProxyEngine on
ProxyPassReverse / https://app.domain.com/

.htaccess RewriteRule php redirect folder to another

I have a folder test. If a person navigates to the folder test, I want the browser to display new.
The folder new doesn't exist on the server, I just want it to be displayed as new, which on the server is linked to the folder test.
This is my config (httpd-vhosts.conf):
<VirtualHost server.com>
DocumentRoot "c:/www/"
ServerName server.com
Alias /new "c:/www/test"
<Directory "c:/www/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
So, to be clear, if someone browses to:
server.com/test (the server uses folder test, but server.com/new is shown in the browser.
server.com/ (the same as above => server.com/new is shown in the browser)
server.com/index.php (the server uses index.php from folder test, browser shows server.com/new/index.php
server.com/test/index.php (the same as above)
server.com/test/folder2/index.php?variable=1 (the server uses index.php from folder test/folder2 and gives the variables to the php file. browser shows server.com/new/folder2/index.php?variable=1)
I have tried different .htaccess rewrites, but I can't get it working.
Maybe I don't have to use the alias?
Don't just give me a link to the apache manual, because I've read it, but can't get it to work...
You don't need Alias for this so comment out:
Alias /new "c:/www/test"
Then have these rules:
<VirtualHost server.com>
DocumentRoot "c:/www/"
ServerName server.com
<Directory "c:/www/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^/?$ /new/ [L,R=301]
RewriteCond %{THE_REQUEST} \s/+test(\S*)\s [NC]
RewriteRule ^ /new%1 [R=301,NE,L]
RewriteRule ^/?new(/.*)?$ /test$1 [L,NC]
</VirtualHost>

Multiples VirtualHost for Symfony3 projects in AWS development environment

We have a problem with access to multiple Symfony3 projects that are in different folders and targeted to the same IP address but in different ports on EC2, for example:
project1:
52.1.1.1:8080/login
/var/www/html/projects/project1/
project2:
52.1.1.1:8181/login
/var/www/html/projects/project2/
Happens that when entering the Project 1 (52.1.1.1:8080) it is displayed correctly, but afterward when accessing to Project 2 (52.1.1.1:8181), this fails, strangely deploys the information from Project 1.
In another scenario, when rebooting the apache service and entering the Project2 (52.1.1.1:8181) all is displayed correctly, but if after entering in the Project2, we enter the Project 1 (52.1.1.1:8080) the information from Project2 is displayed, instead of that of the Project 1.
Ports 8080 and 8181 are open.
Apache configuration on the server is the following:
Listen 8181
<VirtualHost *:8181>
DocumentRoot "/var/www/html/projects/project1/web"
DirectoryIndex app.php
<Directory "/var/www/html/projects/project1/web">
Require all granted
Order allow,deny
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
</Directory>
<Directory /var/www/html/projects/project1>
Options FollowSymlinks
</Directory>
<Directory /var/www/html/projects/project1/web/bundles>
<IfModule mod_rewrite.c>
RewriteEngine Off
</IfModule>
</Directory>
CustomLog /var/www/html/projects/project1/var/logs/reg-access.log combined
ErrorLog /var/www/html/projects/project1/var/logs/reg.error.log
</VirtualHost>
Listen 8080
<VirtualHost *:8080>
DocumentRoot "/var/www/html/projects/project2/web"
DirectoryIndex app.php
<Directory "/var/www/html/projects/project2/web">
Require all granted
Order allow,deny
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
</Directory>
<Directory /var/www/html/projects/project2>
Options FollowSymlinks
</Directory>
<Directory /var/www/html/projects/project2/web/bundles>
<IfModule mod_rewrite.c>
RewriteEngine Off
</IfModule>
</Directory>
CustomLog /var/www/html/projects/project2/var/logs/reg-rm.log combined
ErrorLog /var/www/html/projects/project2/var/logs/reg.error.log
</VirtualHost>
Any help or ideas about what happens?
UPDATED
To access the Project 1, logs the following error:
[2016-08-17 07:33:12] request.CRITICAL: Uncaught PHP Exception Symfony\Component\Debug\Exception\FatalErrorException: "Error: Cannot redeclare class Doctrine\Common\Annotations\Annotation\Target" at /var/www/html/projects/project2/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/Annotation/Target.php line 31 {"exception":"[object] (Symfony\Component\Debug\Exception\FatalErrorException(code: 0): Error: Cannot redeclare class Doctrine\Common\Annotations\Annotation\Target at /var/www/html/projects/project2/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/Annotation/Target.php:31)"} []
"Vendors" is referenced strangely to another project
I found a temporary solution, I had to modify the "app/autoload.php" file
Before
$ loader = require __DIR__.'/../vendor/autoload.php';
After
$ loader = require __DIR__.'/../../project1/vendor/autoload.php';
And it all worked properly.
But I communicated with Javier Eguiluz and he suggested to try installing APC on the server, I installed it, rolled back the changes to the file "app/autoload.php" and it worked properly.
EDIT #2 based on your comment.
Listen 8181
<VirtualHost *:8181>
Servername project1.com
...
Listen 8080
<VirtualHost *:8080>
Servername project2.com
DocumentRoot "/var/www/html/projects/project2/web"
DirectoryIndex app.php
On a remote host edit your hosts file to include the above host names.
For example, if you are on a remote Windows host, edit the file "c:\Windows\System32\Drivers\etc\hosts" as follows:
52.1.1.1 project1.com
52.1.1.1 project2.com
Then in your browser use:
http://project1.com:8181
http://project2.com:8080
This should work. Please investigate first before downvoting. Thanks!

"RewriteEngine On" in virtual host not working

This is my virtual host entry.
Version: Apache/2.2.15 (Unix)
<VirtualHost *:80>
ServerName web1.net
ServerAlias www.web1.net
ServerAdmin webmaster#web1.net
DirectoryIndex index.html index.php
LogLevel warn
ErrorLog /var/log/httpd/web1.net_error_log
CustomLog /var/log/httpd/web1.net_access_log combined
DocumentRoot /web/web1.net/htdocs
<Directory "/web/web1.net/htdocs">
Options Indexes FollowSymLinks Includes ExecCGI
Order allow,deny
Allow from all
AllowOverride All
RewriteEngine On #This is not working
</Directory>
</VirtualHost>
.htaccess file
RewriteEngine on # It works.
#abc5
RewriteCond %{HTTP_HOST} ^example\.org$ [NC]
RewriteRule .* http://www.example.com
When I put the same (RewriteEngine on) in .htaccess file it starts working, why I need require to put this in .htaccess when it is present in virtual host.
Do I need any other changes in Apache config?
You need to have RewriteEngine on in each directory's .htaccess if you want to enable rewriting. So in short:
RewriteEngine on from server config doesn't come into effect for DocumentRoot/.htaccess
RewriteEngine on in DocumentRoot/.htaccess doesn't come into effect for any /subdir/.htaccess