Redirecting from web root and www to a specified subdomain - apache

What's the best way to redirect from web root (e.g., example.com) and the www subdomain (e.g., www.example.com) to a specified subdomain? I've not got great access to the server I'm working on, so editing .htaccess or similar would probably be the most practical solution.

In .htaccess:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^(.*)$ http://subdomain.example.com/$1 [R=301,L]
That'll do a 301 redirect and will include anything after the initial /, so example.com/page.html will go to subdomain.example.com/page.html

If you cannot edit configuration files, but can upload files to the root directory via FTP, you can also do it with severside script. In PHP, for example in an index.php:
<?php header("Location: http://sub.domain.com/"); ?>

Related

htaccess redirect https traffic to a specific folder?

I am hosting my website on godaddy and recently purchased an SSL cert so I can also run https traffic. However, I want all https traffic to go to a specific folder that regular http cannot get to. In addition, I don't want the specific folder to be a sub-folder of the main http document root.
GoDaddy cPanel folder structure:
/home/myAccountFolder/
/cache
/logs
/public_html
/private_html
...
By default, all standard http traffic for "www".foo.com will go to /public_html - but how can I get all traffic for https://api.foo.com to map to the /private_html folder?
I am told that .htaccess can do this but can't seem to locate how to do this. I know I could redirect https to /home/myAccountFolder/public_html/secure, but I want it going to /home/myAccountFolder/private_html - a completely different root folder.
Also, I don't have access to the server configuration or (that I am aware of) have the ability to create/manage virtual hosts within GoDaddy's cPanel enviornment. All I can do is control directories and what goes into my websites .htaccess file.
Inside your site root .htaccess you can use this rule to redirect api.foo.com to secure/ folder:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^api\. [NC]
RewriteRule !^api/ api%{REQUEST_URI} [L,NC]
Inside your api/.htaccess you can use this rule to allow only http://api.foo.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^api\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ - [F]
# rest of the rules

What in .htaccess redirect subdomain apache

What I must to do, when I have got DNS to subdomain and when someone go to blog.example.com the website url is the same but the content is from the main page not from blog folder.
What I should to do? What must be in /var/www/blog/ in .htaccess if anything?
Please help!
You can use the following rule :
RewriteEngine On
#if host is blog.domain.com
RewriteCond %{HTTP_HOST} ^blog\.example\.com$
#then serve all requests from the Root folder.
RewriteRule ^(.*)$ /$1 [NC,L]
Replace blog.example.com with your subdomain in the http_host string.

301 redirect with domain both pointing to same directory

I am currently moving from one domain to another. Both domains are add on domains in CPanel and point to the same sub-directory.
How can i set up 301 redirect that redirects all of the old domains pages to the same file in the new domain. For example olddomain.com/example to newdomain.com/example. Since both domains reside in the same sub directory the both utilize the same .htaccess file.
Since both domains share the same .Htaccess file I'm having trouble getting the 301 redirect to work.
Put the following in the .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^newdomain.com
RewriteRule /(.*)$ http://newdomain.com/$1 [R,L]
It will check what your host-header is, and will redirect anything to newdomain.com if that's not the current site.

Apache mod_rewrite - want a file system redirect not a 301 redirect

I have example1.com on a shared web host running Apache. It has a directory example1.com/foo. I now want example2.com to serve the same content from example1.com/foo, except at the example2.com root without the intervening directory in the URL. Like example2.com/bar.html should serve the same content as example1.com/foo/bar.html .
RewriteEngine on
RewriteCond %{HTTP_HOST} example2.com$ [NC]
RewriteRule ^(.*)$ foo/$1 [NC]
This simple rewrite rule takes any request intended for example2.com and inserts the foo/ to point to the content which is in that directory. Problem is this keeps doing an external 301 redirect. I don't want that, I want the browser to stay on example2.com without redirecting while Apache serves up the content from /foo in the filesystem.
Been over the Apache mod_rewrite docs several times, which say how to force a 301 redirect with the [R] flag but don't say how to force it NOT to happen. What am I missing here? It is behaving the same on both my Linux shared host and a local test with Apache on Windows.
Thanks!
I figured this out. The 301 was happening because I had the directory name wrong in the rule. So the result of the rule pointed to a path that didn't exist, which makes Apache try to fallback from the file system redirect to a 301 redirect.
Then I had to fix an infinite loop, since that above rule always adds "foo" to the URL even if it's already present so I'd get foo/foo/foo/foo/... . We need to add it only if it's not already there. Had to do it with this two-step rule, because you can't use wildcards in a capturing group of a negative rule. But this seems to work, adding "foo" when the host is example2.com and the URL does not already contain "foo".
RewriteEngine on
RewriteCond %{HTTP_HOST} example2.com$ [NC]
RewriteRule !^foo - [C]
RewriteRule ^(.*)$ foo/$1 [NC,PT]

How to redirect to subdomain but then allow normal use of site

So I have my site, www.domain.com.
For a week or so I want to direct all traffic going direct to the site to subdomain.domain.com, a little promo page about an upcoming feature. I want visitors to then be able to continue to the site as normal though after they've read it, so a continue to www.domain.com/index.php link.
How can I do that with in the htaccess file? Everything I've tried so far messes up when clicking the continue link.
Thanks
with .htaccess you could use a 302 temporary redirect, but it would be for a whole sub folder as far as I know.
Another way would be to redirect with JS/server site language to the subdomain, create a cookie, then redirect back to www.domain.com/index.php .
the 302 redirect is explained here: How do I redirect my site using a .htaccess file?
You would need to have a .htaccess for the root folder point to your subdomain
Note that this is only possible if you enable mod_proxy in the Apache config of domain.com otherwise URL will change after redirect.
Enable mod_proxy, mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^index\.php$ http://subdomain.domain.com/ [L,NC,P]