.htaccess only allow access to directory when coming through the right url - apache

Case:
I have a website www.xyz.com/ with folder / abc
I have made a subdomain abc.xyz.com which points (on the webserver) to the /abc folder
I would like to make it impossible for people to access www.xyz.com/abc and be redirected to abc.xyz.com
tried
a normal 301 redirect results in people also being redirected when they go to abc.xyz.com (so error for too many redirects).
Thank you.

I got it after a few trials and errors:
Redirect 301 /abc/ https://abc.xyz.com

Related

Drupal Domain Redirection

We have two domains: www.example.com and www.example.org. Both point to the same site. But, we'd like everything to point to .org. We are running Drupal on a LAMP server hosted by Media Temple.
We have now started to have problems because our .com SSL cert expired, so anybody who goes to the .com site routinely gets scary messages in Chrome, etc.
How can we make it so that if somebody goes to https://www.example.com in google, they get nicely directed to http://www.example.org?
You can do this multiple ways.
.htaccess
RewriteEngine On
Redirect 301 / https://www.exmaple.org
In a PHP file
header("HTTP/1.1 301 Moved Permanently");
header("Location: https://www.example.org");
header("Connection: close");
You can try this module :
https://www.drupal.org/project/domain_301_redirect
This is the domain that all other domains that point to this site will be 301 redirected to. This value should also include the scheme (http or https)
I Hope help you!

Routing all requests from different domain to index.php with htaccess

I've set up a site which uses .htaccess for all URL rewrites, but now i'm trying to set it up so that any domain can be pointed at the site via CNAME record, which works like a charm, heres an example:
mainsite.com - is the site
test.somesite.com points to mainsite.com/index.php
However when you visit test.somesite.com/test it shows 404 because the rewrite rule/directory does not exist. Is there a way to make it in .htaccess so that if the domain is not the main domain, all the traffic points to index.php?

Site Redirection with htaccess creates an infinite loop

Currently we have a number of sites hosted in one GoDaddy account. Each site is inside a separate folder and their respective domains are bound to those folders. The problem is that our main site is in the root of the host and our primary domain is linked to this root. The problem with this setup is that if for example, one of my other sites is in a folder called "secondsite", I can reach the website by going to www.secondsite.com (which is fine) but ALSO by going to www.mainsite.com/secondsite, which we absolutely not want.
The idea is to move all the files of the main site to a folder of their own (let's call it "mainsite"). When I talked to GoDaddy they told me to do a 301 redirect to that folder. I have never worked with .htaccess but I looked up how to redirect and found that I needed to write this:
Redirect 301 / http://mainsite.com/mainsite
However if I do that when I try to access the website I get infinite redirection: http://mainsite.com/mainsitemainsitemainsitemainsitemainsitemainsitemainsitemainsitemainsitemainsitemainsitemainsite
I've looked online and tried to use other solutions, like
RedirectPermanent / http://mainsite.com/mainsite
but the effect is the same.
Not sure what I'm doing wrong.
When using Redirect, you're linking path-nodes together, So:
Redirect / /abc/
means, anything starting with / will go to /abc/, e.g.:
/ -> /abc/
/foo -> /abc/foo
/1/2/3/4/5 -> /abc/1/2/3/4/5
And thus, since you're redirecting back to the same host, the / captures everything and you've got an infinite loop.
Try using either RedirectMatch:
RedirectMatch 301 ^/(?!mainsite)(.*)$ /mainsite/$1
or us mod_rewrite:
RewriteCond %{HTTP_HOST} ^(www\.)?mainsite\.com$ [NC]
RewriteRule ^(?!mainsite)(.*)$ /mainsite/$1 [L,R=301]

Apache: How can i redirect all sites on the server to an URL?

I have a VPS based Centos/cPanel-WHM. I wanna redirect all sites (including all pages & subdomains) on the server to one URL. How can i do this?
create .htaccess file in every website DocumentRoot dir which you want to redirect
# This allows you to redirect your entire website to any other domain
Redirect 301 / http://exampledomain-redirect.com/
At webserver layer (change the .htaccess), you could issue 301 redirects for any requests to your sites to new URL
OR
you could inject javascript (through your web layer) or your code layout framework OR manually
at the head of the page to complete a redirect.
OR
if your domains point to different hosting.. you could upate their NS to point to your new location and do 1 OR 2
.htaccess is the best way, Otherwise change the document root for each site.

replacing domain names using .htaccess

I have a new website as http://abc.com/case_studies/casstudy20/.
I have an old website as http://xyz.com/clients/home.php?client=myclient
There are loads of case studies and stories under old domain that is xyz.com. Now that I am using abc.com is it possible to replace the URL, so it shows abc.com but access the code from the same old place.
meaning when my users goes to old site instead of xyz.com. It should say http://abc.com/clients/home.php?client=myclient. But runs from the same old xyz.com.
Is it possible to do it using htaccess.
Sort of. You can redirect all the traffic from xyz.com to the proper path on abc.com but this requires you to maintain control of both domains.
If you can, it's actually better to put this in the virtual host config for xyz.com then you don't need a complete configuration at all. Assuming you have permissions it will work in .htaccess as well though.
RedirectMatch ^(.*)$ http://www.abc.com$1
If you can keep control of both domains then you are looking for a http 301 redirect. You can do that by adding the following line to your htaccess file to redirect an entire website. Without control of your original domain it's impossible for your old xyz.com website to be routed to the appropriate server so your htaccess file will never have anything to redirect.
redirect 301 / http://abc.com/