htaccess rewrite for wildcard subdomains - php

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.

Related

redirect specific sub domain on specific folder

I have one sub domain api.example.com.
my code is on /var/www/html/ folder.
my all sub domain is redirect to /var/www/html folder.
but i want api.example.com to redirect on /var/www/html/api folder. how it is possible?
I have do some RND but not find any answer related to this.
Right now i am use a DNS for redirection. like this.
*.example.com. 14400 IN A 52.46.171.238.
It is possible using .htaccess or i have to do something with DNS records?.
If you have control over Apache server config then it is easier to do it in VirtualHost by using appropriate DocumentRoot
<VirtualHost *:80>
ServerName api.example.com
DocumentRoot /var/www/html/api
,,,
...
</VirtualHost>
However if you don't have control over Apache server config then do it using a rewrite rule in /var/www/html/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^api\. [NC]
RewriteRule !^api/ api%{REQUEST_URI} [L,NC]

Modify Symfony2 routing to prepend all requests with application name

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).

.htaccess rewrite [virtual] subdomain to [virtual] folder

I have a site with virtual pages where any request is rewrote to the index page in this way:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
I.e, http://www.example.com/job/edit is rewrote as http://www.example.com/index.php/job/edit where I parse it. That works great.
Now, I need to process every virtual subdomain in order that i.e. http://user1.example.com/job/edit becomes http://www.example.com/index.php/user1/job/edit.
I added in CPanel a wildcard subdomain (*.example.com), so every subdomain is pointed to the domain folder (without it I got 'Server not found' on every subdomain).
I tried every example that I found here, but I can't get them to work. If I print out $_SERVER['REQUEST_URI'] on the index page, I allways get the last part, but not the subdomain (i.e. /job/edit).
Is there any way of doing this?
Thanks in advance.
Edit: I already have a workaround for doing it. The $_SERVER['HTTP_HOST'] var contains the whole domain, i.e. user1.example.com. I can parse it in PHP and extract the subdomain, but I think it should be a more elegant way of doing it.
I have Done Same Like this.
Create a Folder called "user"
Create A index.php to handle The calls To user functions
Add A Virtual host in the Httpd.conf That Route The
*.example.com to the Path /public_html/user
<VirtualHost 0.0.0.0>
ServerAlias *.example.com
ServerAdmin webmaster#yourdomain.com
DocumentRoot /home/yourdoma/public_html/user
ServerName yourdomain.com
User yourdoma
Group yourdoma
BytesLog /usr/local/apache/domlogs/yourdomain.com-bytes_log
CustomLog /usr/local/apache/domlogs/yourdomain.com combined
ScriptAlias /cgi-bin/ /home/yourdoma/public_html/joe/cgi-bin/
</VirtualHost>
Now all Call will route to user Folder.
Add a Htaccess file in user folder to rewrite the base path

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