Apache subdomain vhost + subfolders of several Magento 2 installs - apache

Basically I want to have several stores under one subdomain as sub-folders. Like this:
mysubdomain.website.com/magento2v1 - Magento 2 store #1
mysubdomain.website.com/magento2v2 - Magento 2 store #2
mysubdomain.website.com/magento2v3 - Magento 2 store #3
Steps that I did:
This subdomain will be run in a VPS server so first I needed to redirect the subdomain (mysubdomain.website.com, the one above) to another IP (the actual VPS server) using an A record.
Second I created a Vhost with the mysubdomain.website.com on that VPS server
This is the vhost on the VPS server, Apache2:
<VirtualHost *:80>
RewriteEngine On
ServerName mysubdomain.website.com
ServerAlias www.mysubdomain.website.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/mysubdomain
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
And then in /var/www/mysubdomain I have:
/var/www/mysubdomain/magentov1
/var/www/mysubdomain/magentov2
/var/www/mysubdomain/magentov3
Now the problem is, Magento 2 needs .htaccess redirects where it redirects /pub to the actual sub-folder. I can't seem to get these .htaccess to work. The htaccess is read but it seems like the folders go a little bit crazy.
/var/www/mysubdomain/magentov1/.htaccess
/var/www/mysubdomain/magentov2/.htaccess
/var/www/mysubdomain/magentov3/.htaccess
Here's an example of this .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/pub/
RewriteCond %{REQUEST_URI} !^/setup/
RewriteCond %{REQUEST_URI} !^/update/
RewriteCond %{REQUEST_URI} !^/dev/
RewriteRule .* /pub/$0 [L]
DirectoryIndex index.php
Can anybody help me get this .htaccess to work? I think it's related to the folder structure, the folder base, but I don't know how to do this and didn't found any other example.
Thank you very much!

The issue most likely is that you rewrite to the absolute path /pub/ from within the configuration files in the subfolders. How should that rewritten request be processed? After that rewriting step it appears to the next round of rewriting like a request to https://sub.example.com/pub/........ None of your existing configuration files will get applied to that. And it most likely will lead to a http status 404. Which you should be able to see in your http server's error log file and also in your browser's console network tab.
Instead you should rewrite to the relative path pub/instead. Which would result in the next rewriting step getting applied to the request to https://sub.example.com/magentov2/pub/........ Which would again get (correctly) served from within that subfolder.
In general you should try to keep global rewriting rules in a common place instead of doubling them in various parallel configuration files. Even better than a common distributed configuration file (".htaccess") in the http hosts DOCUMENT_ROOT folder would be to implement such rule in the actual central configuration file. That is faster, more robust, more secure and easier to debug.

Related

.htacces files doesnt work despite of AllowOverride All

I dont know why but my simple .htaccess file doesnt work on my server.
/var/www/html/.htaccess
RewriteEngine On
#RewriteCond %{HTTP_HOST} ^/$ [R=301,L]
#RewriteRule ^(.*)$ htt://www.onet.pl/$1 [L]
RewriteRule ^/lol$ /dev/public
I tried to make a test with either redirection of all traffic to another server or simple redirection myaddress/lol to myadress/dev/public where i have index.html file.
Modul REWRITE is already enabled
#a2enmod rewrite
Module rewrite already enabled
And this is my /etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
<Directory /var/www/html>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
When i write rubbish in /var/www/html/.htaccess and open servers main address then it throws error message and in apache logs is see some error about wrong command in .htaccess file.
But neither of redirections placed in .htaccess works. Second redirection does not redirects from adress/lol to address/dev/public - browser just throws that there is no /dev site.
I have root access, server is Debian 8 with apache 2.4.10. What can i do?
The simple issue us that you are trying to use an absolute path with a rewrite rule inside a dynamic configuration file. It is clearly documented that in this case you need a relative path due to the nature of how those stupid dynamic configuration files work (.htaccess).
Take a look at the version using a relative path:
RewriteEngine On
RewriteRule ^lol$ /dev/public
A more intelligent way to handle this discrepancy is to use a pattern that will work in both case, in dynamic configuration files and in the real host configurations:
RewriteEngine On
RewriteRule ^/?lol$ /dev/public
But the best idea would be not to use such dynamic configuration files at all but the http servers host configuration instead. Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).
Here is the official documentation (which you definitely want to read), take a look at the section labelled "Per-directory Rewrites":
http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Apache dynamic wildcard host rewrite with dynamic subdomains

I'm currently in the process of converting an old apache vhost, where one is currently created for each new release, to a dynamic host that can handle all the subdomains so we don't have to create a new one all the time. I need some help! We're using apache v2.2.
The Goal
Dynamic all the things. To have a single virtual host that handles all redirects for a specific set of subdomains. The url is below, note that sub1 and branch are dynamic and could be anything.
sub1.branch.sandbox.domain.com
The directory structure for this is as follows:
/var/www/vhosts/branch/sub1.branch.sandbox.domain.com
As you can see above, the directory structure has the branch as a sub-directory before the full url is the name of another sub-directory.
Also, /images/ in the url needs to forward to shared-httpdocs/images for each domain.
The vhost I have so far
<VirtualHost *:80>
ServerName branch.sandbox.domain.com
ServerAlias *.branch.sandbox.domain.com
VirtualDocumentRoot /var/www/vhosts/branch/%0
Options Indexes FollowSymLinks
# Rewrite Engine Stuff Here
RewriteEngine On
DirectoryIndex index.php
# Assets needs to be forwarded - currently not working
RewriteCond %{REQUEST_URI} ^/(css|js|images)/.*
RewriteRule .*$ /var/www/vhosts/%0/shared-httpdocs/%1$ [L]
# The HTTP dispatcher - currently not working
RewriteCond %{HTTP_HOST} ^(.*)\.branch\.sandbox\.domain\.com [NC]
RewriteRule ^(.*)$ /var/www/vhosts/%1/applications/portal/dispatchers/http.php [L,QSA,NS,NE,NC]
</VirtualHost>
The old host I'm trying to copy
This is the old vhost I'm trying to convert from. It's horrible, messy, and our ops has to create a new DNS entry every time. What a joke! I need to sort this out...
<VirtualHost *:80>
# Notice how the stupid convention below will require a new DNS entry each time?
ServerName sandbox.branch.domain.com
ServerAlias sandbox.api.branch.domain.com
DocumentRoot /var/www/vhosts/sandbox.branch.domain.com/applications/portal/httpdocs
<Directory "/var/www/vhosts/sandbox.branch.domain.com/applications/portal/httpdocs">
allow from all
order allow,deny
# Enables .htaccess files for this site
#AllowOverride All
RewriteEngine On
# Rewrite all non-static requests to go through the webapp
RewriteCond %{REQUEST_URI} ^/(css|js|images)/.*
RewriteRule .* - [L]
# Rewrite everything else to go through the webapp
RewriteRule ^(.*)$ /dispatchers/http.php [QSA,L]
</Directory>
<Directory "/var/www/vhosts/sandbox.branch.domain.com/applications/portal/dispatchers">
allow from all
</Directory>
# Allow us to rewrite to the webapp without it being in the webroot
Alias /dispatchers /var/www/vhosts/sandbox.branch.domain.com/applications/portal/dispatchers
# Get shared/ to point to the shared static resources
Alias /shared /var/www/vhosts/sandbox.branch.domain.com/shared-httpdocs
</VirtualHost>
A new DNS entry is required each time we have a new branch, so I'm trying to mitigate this by providing a dynamic subdomain vhost (see the vhost I have so far). I've gone from not even being able to match /images/ in the url to a permanent redirect loop.
How can I achieve my goal? I know it's a little complex. If I can't do it, I'll just have to write a script that will generate a new vhost each time but a dynamic one that 'just works' would be fantastic. I've put two days into this so far, I'm no sysadmin. Your help would be greatly appreciated.
Resources I have been using:
mod_rewrite official docs - Shows the basics like things on conditions with REWRITE_COND
Sub domain rewriting - A question on subdomain rewriting
Asset rewriting - Another question on rewriting things like images / css / js, which doesn't seem to work for me
It's not a complete answer, but is too long for comment.
The %0 (%0 to %9) in a rewrite rule are back references to captures in the last RewriteCond. It seems to me you wanted instead the host name. Also it seems you miss the "branch" part of the path. In the Asset's rewrite you also throw away the filename part.
# Assets needs to be forwarded - currently not working
RewriteCond %{REQUEST_URI} ^/(css|js|images)/(.*)
RewriteRule .*$ /var/www/vhosts/branch/%{HTTP_HOST}/shared-httpdocs/%1/%2$ [L]
# The HTTP dispatcher - currently not working
RewriteCond %{HTTP_HOST} ^(.*)\.branch\.sandbox\.domain\.com [NC]
RewriteRule ^(.*)$ /var/www/vhosts/branch/%{HTTP_HOST}/applications/portal/dispatchers/http.php [L,QSA,NS,NE,NC]
You can get debugging help also from mod_rewrite dedicated logging with RewriteLog and RewriteLogLevel directives.
Hope this will bring you further.

Forward a subdomain to a subfolder and preserve subfolder's .htaccess mod_rewrite rules

I have a standard LAMP setup and I'm trying to make subdomains automatically work as long as the appropriately-named folder exists.
If you're familiar with MediaTemple's GridServer shared hosting service, I'm trying to emulate the way it handles wildcard subdomains:
If subdomain.domain.com is requested, look for a folder named subdomain.domain.com
If the folder is found, call it home and serve up the site inside of it, obeying any .htaccess files that may be in there.
If no folder is found, just display domain.com
I've been told a .htaccess mod_rewrite in my root domain is the way to go, and I've been able to detect subdomains and at least point to the appropriate subfolder, but I don't think this leaves any opportunity for the subdomain.domain.com .htaccess file's own mod_rewrite to take over once the server knows where the subdomain's folder is located.
Here's my .htaccess for that:
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9-]+)\.domain.com$
RewriteRule ^(.+) /vhosts/%1.domain.com/html/pages/$1.php [L,QSA]
...And my folder structure:
domain.com
html
.htaccess
vhosts
subdomain.domain.com
html
.htaccess
pages
index.php
pagename.php
So as you can see, the site inside subdomain.domain.com is dependent on it's own mod_rewrite in order to function, since the page files aren't where the server expects them.
Now, I know I could probably include the subdomain's .htaccess rules in my root domain's .htaccess with the appropriate conditions, but the kicker is that I need to be able to point completely different domain names to these subdomains too (so the subdomain is accessible via subdomain.domain.com and mydomain.com), so these subdomain.domain.com folders need to be totally self-sufficient.
So how can I get my server to look in the correct location for a subdomain's folder, while allowing its own .htaccess mod_rewrites to work?
Any help is greatly appreciated!
Just had someone suggest that mod_rewrites are the wrong way to go about it, and mod_vhs is what I want to use. Does anyone know about mod_vhs?
I got it to work using VirtualHosts:
NameVirtualHost *
# Root domain homepage
<VirtualHost *>
DocumentRoot /var/www/domain.com/html
ServerName domain.com
ServerAlias www.domain.com
</VirtualHost>
# Hosted sites
<VirtualHost *>
VirtualDocumentRoot /var/www/vhosts/%0/html
ServerName *
ServerAlias *
</VirtualHost>
The first entry catches requests to my home page and serves up the standard site, while the second catches EVERYTHING else and routes to a different location based on the host name requested.
Easy. Just make sure the subdomain is registered with your server's DNS, then point it with an htaccess like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?sub\.domain\.com
RewriteRule .* /subfolder [L]
No need to mess with the subfolder's htaccess.

Apache multiple URL to one domain redirect

For the last two day, I've been spending a lot of time to solve my problem, maybe someone can help me.
Problem: I need to redirect different url's to one tomcat webbase-dir used for artifactory.
following urls should point to the tomcat/artifactory webapp:
maven-repo.example.local ; maven-repo.example.local/artifactory ; srv-example/artifactory
Where maven-repo.example.local is the dns for the server-hostname: "srv-example"
I'm accessing the tomcat app through the JK_mod module. The webapp is in the ROOT directory
This is what I've got so far:
<VirtualHost *:80>
#If URL contains "artifactory" strip down and redirect
RewriteEngine on
RewriteCond %{HTTP_HOST} ^\artifactory\$ [NC]
# (how can I remove 'artifactory' from the redirected parameters? )
RewriteRule ^(.*)$ http://maven-repo.example.local/$1 [R=301,L]
ServerName localhost
ErrorLog "logs/redirect-error_log"
</VirtualHost>
<VirtualHost *:80>
ServerName maven-repo.example.local
ErrorLog "logs/maven-repo.example.local-error.log"
CustomLog "logs/maven-repo.example.local-access.log" common
#calling tomcat webapp in ROOT
JkMount /* ajp13w
</VirtualHost>
The webapp is working with "maven-repo.example.local", but with "maven-repo.example.local/artifactory" tomcat gives a 404 - "The requested resource () is not available."
It seems that the mod_rewrite doesn't have taken any effect, even if I redirect to another page, e.g google.com
I'm testing on windows 7 with maven-repo.example.local added in the "system32/drivers/hosts" file
Thanks in advance!
Thanks a lot for your hint #PHP-Prabhu
a simple:
RedirectPermanent /artifactory /.
in the apache httpd.conf did the trick!
First, all Redirects are processed before Aliases are processed, and therefore a request that matches a Redirect or RedirectMatch will never have Aliases applied. Second, the Aliases and Redirects are processed in the order they appear in the configuration files, with the first match taking precedence
Please see this URL
http://httpd.apache.org/docs/2.1/mod/mod_alias.html

Problems redirecting old domain to new with Apache and htaccess

My homepage is located at www.nazgulled.net and I bought a new domain which is www.ricardoamaral.net. Both these domains point to the same exact server.
I'm having two problems with the redirection, my current code is this:
RewriteCond %{HTTP_HOST} ^(www\.)?nazgulled\.net [NC]
RewriteRule ^(.*)$ http://www.ricardoamaral.net/$1 [L]
For now I'm just testing but the idea is to replace [L] with [L,R=301] when I'm ready to move the whole thing, just so you know.
Anyway...
1) This is not working, when I try it, I can't access "nazgulled.net", it gives me a "server internal error" and I don't understand why... I don't understand why because if replace "ricardoamaral.net" by "google.com", the redirect works just fine :/
2) I have a few subdomains and I would like to redirect everything in those too. My first choice is to add different rewrite conditions/rules for each of the subdomains but that takes a lot of manual code and if the user types some subdomain that doesn't exist, they don't be redirect it and I also want that.
I think for your situation creating separate vhosts would be the ideal fit. What I do often is place domains that are being redirected on the same server but in a different folder to keep the www folder clean, for instance. My main site would be here:
/var/www/example.com/public
For all my redirects I would place them like so:
/var/www/redirects/example-2.com/public
Within each public folder of the redirected sites you would add this line to your .htaccess file
RedirectMatch permanent /.* http://example.com/
For 1, you can use something like this instead:
<VirtualHost *:80>
ServerAdmin webmaster#nazgulled.net
ServerName nazgulled.net
ServerAlias www.nazgulled.net
Redirect permanent / http://www.ricardoamaral.net/
</VirtualHost>
I use a similar configuration and it works perfectly, keeping query parameters, paths, etc.
Regarding your own rewrite rule, I want to note that you may lose your query params if you don't include QSA.
To diagnose internal server errors, just have a look at the log file provided by your ErrorLog directive within your vhost, or a global one, depending on your configuration. It should give you the exact reason for the error.
This is best solutions. Create a notepad file and save it as .htaccess if you do not already have an existing one.
Update the .htaccess file with the following code and save
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://new-domain.com/$1 [R=301,L]