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).
Related
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]
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
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
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).
I've been searching similar solutions but none of them seems working on my server.
What I'm trying to do is set redirect from yyy.zzz.com (subdomain) to zzz.com (primary domain) without rewriting url. So both yyy.zzz.com and zzz.com actually pointing at same directory and same files in it.
Currently I have this .htaccess:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^yyy\.zzz\.com$ [NC]
RewriteRule ^(.*)$ http://zzz.com [L,NC]
Which, of course, just redirects straight on, and url actually changes.
Just for information, I want to set such subdomain url for CMS, so if user wants to enter CMS he does it from subdomain, while actually only $_SERVER['HTTP_HOST'] is changing.
Any help would be appreciated.
You should set up a ServerAlias in your apache config. Setting yyy.zzz.com as an alias for zzz.com
<VirtualHost *>
ServerName zzz.com
ServerAlias yyy.zzz.com
# ...
</VirtualHost>
http://httpd.apache.org/docs/2.2/mod/core.html#serveralias