redirect specific sub domain on specific folder - php

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]

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.

php links not working in sub folder of multi site

Testing two identical folders , one placed in a Drupal install folder and the other in a plain html subfolder in
home root , only the php links for the one in CMS install work. This makes sense because Drupal has a settings php
file and .htaccess file which makes sure everything is in its proper place for links to work whether in home root or not. However I thought it would be easy enough to get the links to work in plain html subfolder with a simple rewrite
rule in the .htaccess file which exists in the php folder. Yet try as I might, nothing has worked so far.
The configuration in /etc/httpd/conf.d/my.conf
<VirtualHost *:80>
ServerName drupalsite.com
ServerAlias www.drupalsite.com
ServerAdmin vps#drupalsite.com
DocumentRoot "/var/www/html/drupalsite.com"
<Directory /var/www/html/drupalsite.com>
AllowOverride All
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName plainhtml.com
ServerAlias www.plainhtml.com
ServerAdmin vps#plainhtml.com
VirtualDocumentRoot "/var/www/html/plain”
</VirtualHost>
The .htaccess file in identical php folders
RewriteEngine On
RewriteBase /phpfolder/
RewriteRule ^c-(.*)$ catpost.php?id=$1 [L]
RewriteRule ^a-(.*)-(.*)$ archives.php?month=$1&year=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
So drupalsite.com/phpfolder links all work
But plainhtml.com/phpfolder links do not work
phpfolder for Drupal resides in /var/www/html and Drupal site is symlinked
phpfolder for plain html subfolder to root resides in /var/www/html/plain
Both phpfolders are owned by apache:root and all files within are owned by root:root
Have also tried changing the .htaccess rewrite rule to:
RewriteBase /
RewriteBase /plain/
RewriteBase /plain/phpfolder/
You will have to define a Directory entry with AllowOverride for:
/var/www/html/plain
<Directory /var/www/html/plain>
AllowOverrideAll
Allow from all
</Directory>
Note: If you have access to the main configuration file of the server you shouldn't be using .htaccess, configuring Rewrites or Redirects in Virtualhost is simpler. As you have already seen, using sub-files and per-dir configurations make things more complicated, not counting the overhead of apache having to constantly check those files when AllowOverride is enabled.

Redirect subdomain without changing url in htaccess

my .htaccess
RewriteCond %{REQUEST_URI} ^/(poodle|mandy|sandy|nova|wendy) [NC]
RewriteRule ^.*$ /wendy/index.php#%1 [L]
here I am able to redirect all folders without changing url.
But when I am trying to go redirect subdomain
like
wendy.xxxxxx.com/ggsgs/sfghdsgd.html
I am getting Internal Server error.
any way I can redirect all subdomains to wendy/index.php without changing url.
Thanks.
You shouldn't manage domains redirection in .htaccess. It should be configured in apache config using VirtualHosts like this:
<VirtualHost *:80>
Servername name1
ServerAlias name2
ServerAlias name3
[...]
</Virtualhost>
If you want to redirect ANY sub domain, you can use wildcard:
ServerAlias *.example.com

htaccess invisibly rewrite folder

I have multiple-domains with different content on one server in the directory /html/, which serves all domains, like example1.com, example2.com, example3.com, example4.com.
This works fine.
I now need to have 2 different(!) wordpress-installations. From the outside I would like to have:
example1.com -> No Blog
example2.com/blog/
example3.com -> No Blog
example4.com/blog/
The internal path-structure on the server should look like:
/html/
/blog-for-server2/
/blog-for-server4/
Because otherwise the content-management-system in /html/ gets messed up. In fact this is almost like having independent subdomains for the blogs and pointing them to different pathes on the server (but I don't want to use subdomains).
Any ideas?
Assuming you've already created the domains in Apache's httpd-vhosts.conf file you can create the redirects inside that, something like:
<VirtualHost *:80>
ServerName example1.com
DocumentRoot /home/servers/common_root/html
RewriteEngine On
RewriteRule ^/blog/? /blog-for-server-1
</VirtualHost>
<VirtualHost *:80>
ServerName example2.com
DocumentRoot /home/servers/common_root/html
RewriteEngine On
RewriteRule ^/blog/? /blog-for-server-2
</VirtualHost>
Otherwise you can do it in a root .htaccess but it's messy:
RewriteEngine On
RewriteCond %{SERVER_NAME} =example1.com
RewriteRule ^blog /blog-for-server-1
RewriteCond %{SERVER_NAME} =example2.com
RewriteRule ^blog /blog-for-server-2

Htaccess Rewrite to PHP File in Parent Directory

I use a custom framework which has many sub applications. I want to assign a domain to each application folder but ran into an issue.
Apache vhosts config
<VirtualHost *:80>
ServerName app1.com
DocumentRoot /var/www/framework/public/app1
</VirtualHost>
<VirtualHost *:80>
ServerName app2.com
DocumentRoot /var/www/framework/public/app2
</VirtualHost>
/var/www/framework/.htaccess
DirectorySlash Off
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)/?$ index.php?_ROUTE=$1 [QSA]
/var/www/framework/index.php
<?php
exit('WORKED!');
//Route request
//...
Everything's works fine except that it tries to use the index.php file in the document root such as "/var/www/framework/public/app1/index.php" when actually I want to use the index file "/var/www/framework/index.php".
So how would I get it to use the index.php file two directories above or relative to the .htaccess location?
You don't. The .htaccess will only be read starting at the root folder of the virtualhost.
You either need to clone the file (ugly unless you're using something like a source code repository to pull it as an external) or add it to your vhosts config. I'd recommend just making one 'rewrite.conf' file and loading it through the vhosts config through include.
vhosts conf:
<VirtualHost *:80>
ServerName app1.com
DocumentRoot /var/www/framework/public/app1
Include rewrite.conf
</VirtualHost>
<VirtualHost *:80>
ServerName app2.com
DocumentRoot /var/www/framework/public/app2
Include rewrite.conf
</VirtualHost>
rewrite.conf:
DirectorySlash Off
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)/?$ index.php?_ROUTE=$1 [QSA]
This does have a few drawbacks:
You'll need to reset the server every time you change it.
If you have a lot of rewrites, you'd probably want to keep this somewhere at the code level... however, I'd argue that you probably want a different solution (as in, a PHP handler) if you have a whole mess of rewrites.
Note that you could also just do the same rewrite directly in the .htaccess, but if you're going to do it, lower level seems better.
What you're doing is essentially a multi-site application.
You should set the document root to the folder that contains the main index.php file. Then that should route the request to the required app by checking the HTTP host property in the $_SERVER superglobal, like for example:
switch ($_SERVER['HTTP_HOST']) {
case 'app1.com':
include 'app1/index.php';
// Do stuff
break;
case 'app2.com':
include 'app2/index.php';
// Do stuff
break;
}

Categories