Modify Symfony2 routing to prepend all requests with application name - php

I want to host two instances of the same Symfony2 application on one Apache server. Each application should be addressed like so:
http://myserver/app1/
http://myserver/app2/
To do so I have set up two port based virtual hosts in Apache plus the third one on port 80 that redirects folder requests to proper vhosts:
<VirtualHost *:80>
DocumentRoot /var/www
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/app1/(.*)$
RewriteRule ^ http://%{HTTP_HOST}:9999/%1 [L]
RewriteCond %{REQUEST_URI} ^/app2/(.*)$
RewriteRule ^ http://%{HTTP_HOST}:8888/%1 [L]
</VirtualHost>
<VirtualHost *:9999>
DocumentRoot /var/www/app1/web
<Directory /var/www/app1/web>
</Directory>
</VirtualHost>
<VirtualHost *:8888>
DocumentRoot /var/www/app2/web
<Directory /var/www/app2/web>
</Directory>
</VirtualHost>
That works perfectly as http://myserver/app1/ is redirected to http://myserver:9999 and http://myserver/app2/ is redirected to http://myserver:8888 (the URL in browser changes). I want the URL in web browser to stay as http://myserver/app1 or http://myserver/app2.
I can do this by adding switch 'P' to RewriteRule, like so:
RewriteRule ^ http://%{HTTP_HOST}:9999/%1 [L,P]
This however creates problem, because now Symfony sees all requests to be coming from port 80 and redirects them back to that port not to the proper virtual host. I think one way to fix this issue would be to force Symfony2 to prepend all requests with the proper application name.
I tried to do so by modifying generateUrl method in vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Controller\Controller.php to always add application name in front of generated URL and it worked but only for page requests (assets were missing for example).
So my question is if there is a way to full Symfony into thinking that all requests should go back to proper 'app' URL?

My suggestion is that you don't try to do this via Apache configuration. You can configure each Symfony application to live in a subfolder, see:
http://www.yegods.it/2015/01/30/install-symfony-app-in-a-subfolder-of-an-existing-site/
You only need to edit app.php, app_dev.php and composer.json (for assets).

Related

htaccess rewrite for wildcard subdomains

few hours later, i need help. I trie to rewrite the url for using wildcard subdomains.
My domains will be *.domain.com - for example mytest.domain.com
For every subdomain is a subfolder unter www.domain.com/sdom/content/mytest
I've setup the apache/nginx server. For testing i upload a index.html inside the "content" folder.
If i type mytest.comain.com this index.html will display. So now i upload the .htaccess file inside this folder to route from here to the destinations (in this example to the "mytest" folder).
I trie a lot of thiks, at least this
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$
RewriteRule (.*) /httpdocs/domain/sdom/content/$1/index.html
i call mytest.domain.com but it wont go to the subfolder, it go to the maindomain.
If i delete the htaccess file, the index.html of the "content" folder will display.
After rewriting the url in the adressfield of the browser shoul display mytest.domain.com - not the new path to the subfolder etc.
What i have to do, that is works ?
UPDATE
subdomains route to -> mainfolder
subdomains should route by htaccess to folder that has the same name of the subdomain
So i trie htacess
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.tld$
RewriteRule ^$ /subdomainfolder/index.html
With .htaccess files you can't. Here mytest.domain.com and www.domain.com are different hostnames. So inevitably an external redirect will happen.
If you want different domains run by one physical server you should use virtual hosts:
<VirtualHost *:80> # Change this to *:443 if you use SSL/TLS
ServerName mytest.domain.com # Your domain name
DocumentRoot $SRV_ROOT/httpdocs/domain/sdom/content/mytest/ # assuming $SRV_ROOT is set to root of your server
<Directory "$SRV_ROOT/httpdocs/domain/sdom/content/mytest/">
Require all granted # on Apache 2.4
# Below are instructions for Apache 2.2
Order Allow,Deny
Allow from all
</Directory>
</VirtualHost>
You'll need to restart your server each time you add a subdomain, as well as having a DNS record or local record in /etc/hosts file if you test locally.

Rewrite Rule for Domain Aliases on Apache

I have a situation where in I have my main domain name as :
www.example1.com and I have an alias domain www.getexample1.com
Now, if someone hits www.getexample1.com, I wish to redirect to www.example1.com/newpage ; but www.example1.com should not undergo that redirection.
I tried using virtual host for this but it went into a redirect loop. Is there a RewriteCond and RewriteRule that can help solve the above issue?
Attempted Virtual Host Code:
<VirtualHost *:80>
# This first-listed virtual host is also the default for *:80
ServerName www.example1.com
ServerAlias getexample1.com
Redirect / https://www.example1.com/newpage
DocumentRoot "var/www/html"
<Directory "/var/www/html">
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
I wish to only redirect getexample1.com to the redirect address, and not my name server domain. How can that be achieved?
Definately.
This should do the trick:
RewriteCond %{HTTP_HOST} ^www\.getexample1\.com$ [NC]
RewriteRule ^/?(index.(php|html))?$ http://www.example1.com/newpage [R=302,L]
Now, ^www\.getexample1\.com$ could also be ^(www\.)?getexample1\.com$ for matching both, www and no-www domain.
^/?(index.(php|html))?$ will match /, index.php and index.html, you can play around with this regex to, according to your requirements.
I used a 302 redirection for testing purposes, you can set it as a 301 redirection when you're sure that you have coded the rules you want (to prevent webbroser redirection caching issues).

Laravel assets, routes and pages

I think this is an easy one, but i'm really confused myself.
myproject.com/index.php shows my project main page
myproject.com shows my project main page too.
In 1st case, after routing it works fine (i.e. : myproject.com/index.php/register)
In 2nd case, after routing it fails (i.e. : myproject.com/register) with following error : The requested URL /register was not found on this server.
So because of that, i thought that i had to re-route every request for myproject.com to myproject.com/index.php
app/config/app.php :
'url' => 'http://127.0.0.1/public',
etc/httpd/conf/httpd.conf :
<VirtualHost *:80>
DocumentRoot "/var/www/html/myproject/public/"
ServerName myproject.com
</VirtualHost>
Nothing's been configured in hosts file.
Goal: how can i re-route all requests from myproject.com to myproject.com/index.php with hiding index.php part of from users ?
Visitors should see that : myproject.com
but i want them to actually reach : myproject.com/index.php
Workaround:
<VirtualHost *:80>
DocumentRoot "/var/www/html/myproject/public/index.php"
ServerName myproject.com
</VirtualHost>
Result : I cannot reach the assets, (js, css files) results with 404 because my browser is not allowed to reach myproject.com/public/. Root has been set as myproject.com/public/index.php in this virtualhost settings.
Check if the .htaccess file is present in your public directory.
If not check the Pretty Url Section.
Also make sure that you put <Directory> directives in your vhost configuration just to be on the safe side.
Eg:
<Directory "/var/www/html/myproject/public/">
AllowOverride All
Allow from All
</Directory>
Concerning the 'url' => 'http://127.0.0.1/public', in app.php am pretty much sure the /public is kinda extra.
'url' => 'http://127.0.0.1', should be working fine.
You need rewrite rules for this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Laravel docs: http://laravel.com/docs/5.1#pretty-urls

virtual subdomain: one subdomain per user

On my website i'm using virtual host so my users can have virtual domain like 'user1.mydomain.com', 'user2.mydomain.com',...
The problem is that on virtual domains like 'user1.domain.com' the index page is always the same as on my index page 'http://mydomain.com'.
What i want to do is to have two different index pages for the domain and for subdomains. My question, how to have subdomains redirected to 'index2.php' (for example) and still have the subdomains to look like 'user1.mydomain.com'?
Ideally, you would set up completely separate document directories for each subdomain. vhost in apache is the way to go. If you however want to do it your way (subdomains redirecting to individual files), then it's a bit more work, but is still doable. First, define the mod_vhost with wildcard:
<VirtualHost 111.22.33.55>
DocumentRoot /www/subdomain
ServerName www.mydomain.com
ServerAlias *.mydomain.com
...
</VirtualHost>
Then inside this VirtualHost setup rewrite rules using mod_rewrite:
<Location "/">
RewriteCond %{HTTP_HOST} ^user1.mydomain.com$
RewriteRule ^\/$ http://www.mydomain.com/index2.php [R=301,L]
RewriteRule ^\/index.php$ http://www.mydomain.com/index2.php [R=301,L]
RewriteCond %{HTTP_HOST} ^user2.mydomain.com$
RewriteRule ^\/$ http://www.mydomain.com/index3.php [R=301,L]
RewriteRule ^\/index.php$ http://www.mydomain.com/index3.php [R=301,L]
...
</Location>
Note however that this will only work properly for / and /index.php requests to subdomains. You are much better off setting up separate document root directories for each subdomain if you intend to do anything more than this.
Maybe you are interested in this.
http://httpd.apache.org/docs/2.2/mod/mod_vhost_alias.html#virtualdocumentroot
This allow, in Apache server, to automatically use a document root based in the subdomain
I would use php to check the url.
If it is a subdomain include index2.php else include index1.php
Without knowing the servers you are running on it is the only method I could think of.

Remove the path part to access my website

I got a rather easy problem. How do I remove the path to my website called domain.com?
I've got a lampp server where I put one folder called "domain" in the htdocs folder and to access my website I need to write domain.com/domain instead of just domain.com. And when I write domain.com I come to the usual xampp website
There are a few options, but the easiest is probably using some URL rewriting given your current setup.
mod_rewrite in .htaccess
In your htdocs folder create a .htaccess file:
RewriteEngine On
RewriteBase /
# If the URL does not already exist as a file
RewriteCond %{REQUEST_FILENAME} !-f
# or directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /domain/$1 [QSA,L,NC]
Virtual hosting with Apache
You could also edit your VirtualHost container in the Apache configuration to change the directory as well. If you are running XAMPP then I have previously written about using virtual hosts with it on my blog.
For example:
<VirtualHost *:80>
ServerAdmin name#domain.com
DocumentRoot c:\xampp\simonholywell.com\pub
ServerName simonholywell.localhost
<Directory c:\xampp\simonholywell.com\pub>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
A development server for teams (or individuals)
I have also written a large article on setting up a development or staging server for a team as well, which would be good reading if you are setting up such a system. It allows you to simply add a folder on the server and it is immediately available as a subdomain without any further configuration. This is something called mass virtual hosting.
You need to add a virtual host, check the apache name based virtual host documentation. If you're running on windows you could follow this guide, if you're on linux/unix you could try the following guide

Categories