Internally rewrite a subdomain to main domain - apache

I am trying to internally rewrite URLs of a subdomain:
e.g. sub1.domain.com/something/ silently goes to to main domain domain.com/sub1/something/.
I have been able to get this to work properly as a redirect. However, I do not want them to redirect.
I still want it to show sub1.domain.com/something/ as the URL and simply display the content from domain.com/sub1/something/.
Need some help achieving this. Here's what I have so far:
RewriteCond %{HTTP_HOST} ^sub1\.domain\.com [NC]
RewriteRule (.*) http://domain.com/sub1/$1 [L]
Note: both domains are pointing to the same server (and i can make them share the same document root folder, if needed)

Related

Apache Mod_Rewrite needs to keep the Path but change the Domain

Background
I have a website that has several other alias domain names, in an effort to streamline and simplify I want all the domains to revert to the one domain name I now advertise -
Main name: www.explorerWorld.co.uk
Other names that load the same content but retain their own address:
www.exploreElsewhere.co.uk
www.exploitedexplodedexplorers.co.uk
what I have is that across the internet there are links in peoples blogs and elsewhere that go to specific pages on my site, but these will be under these different domains.
I have changed the domain Alises under cPanel and this works, for the base address only, but a domain for example www.exploreElsewhere.co.uk/trees.php does not redirect.
Question
I would like these pages to still work but to redirect to the main site -- www.explorerWorld.co.uk -- * but preserving their file path*
so: www.exploreElsewhere.co.uk/plants/trees.php gets seemlessly changed to www.explorerWorld.co.uk/plants/trees.php
My htaccess so far:
RewriteCond %{HTTP_HOST} !^exploreWorlds\.co\.uk$1 [NC]
RewriteRule ^(.*)$ http://www.exploreWorlds.co.uk/$1 [R=301,L]
But this seems to work in an endlessly repeating loop, How can I improve this htaccess ?
I have also looked here but this was not very helpful:
.htaccess change domain but keep path
I have found a solution to this problem was to use %{REQUEST_URI} which is the path given to the server.
(with a pointer from Arco444)
RewriteCond %{HTTP_HOST} !^(www\.)?exploreworlds\.co\.uk [NC]
RewriteRule ^(.*) http://www.exploreworlds.co.uk/%{REQUEST_URI} [R=301,L]
This successfully rewrites the domain part of the URL to force alias URL links to be redirected to my base website address + path
You have a mistake in your RewriteCond. Try
RewriteCond %{HTTP_HOST} !^www.exploreWorlds\.co\.uk$1 [NC]
RewriteRule ^(.*)$ http://www.exploreWorlds.co.uk/$1 [R=301,L]
instead, or remove the tilda from your original snippet

.htaccess 301 redirects based on TLD

We're in the process of switching our current site from a multiple domain configuration into a single domain, multiple folder format. i.e.
.co.uk/<uri> is becoming .com/en-gb/<uri>
.com/<uri> is becoming .com/en-us/<uri>
I'm hoping that I'll be able to handle this via a couple of well-crafted .htaccess rules, but I'm not sure of code I'm going to need to achieve this. Can you help?
(PS, I've left the actual domain blank, as we only need to test for the TDL, not the entire domain - although whatever the original domain was need to stay the same, with only the TLD changing - i.e. whatever.co.uk would redirect to whatever.com/en-gb/, whatever2.co.uk would redirect to whatever2.com/en-gb/, and whatever.com would redirect to whatever.com/en-us/)
add the following directives to your .htaccess:
RewriteCond %{HTTP_HOST} (.+)\.co\.uk$
RewriteRule (.*) http://%1.com/en-gb/$1 [R=301,L,QSA]
RewriteCond %{HTTP_HOST} \.com$
RewriteCond %{REQUEST_URI} !^/(en-us|en-gb)/
RewriteRule (.*) /en-us/$1 [R=301,L,QSA]
if you have so many domain TLDs, you maybe want to use RewriteMap to avoid duplicating the first rule for every TLD, RewriteMap will map TLD to Uri string (ex: .co.uk to en-gb),

htaccess redirect for global subdomains system - all subdomains redirected to one php file to handle them all

i really could use some advices related to subdomains.
recently we decide to make every subdomain of main portal to be available for blogs (something similar to lets say any blog platform)
so what i cant achieve is to make redirection to certain folder of main portal site or php file like:
xxxxx.domain.com/index.php?get=xx&get2=xx -> to be run by actual php file located on main portal account, lets say subdomains.php?get=x&get2=xx, alternatively - to be run by mainportal/subdomains/index.php?get=x&get2=xx
i tried with
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.pl$ [NC]
RewriteRule (.*) subdomain.php [L]
effect was almost correct but get parameters are missing:(
on the other hand it can be entirely wrong... i simply dont understand or that rewrite rules:(
thanks for any help/tips
You can try with QSA (query string append):
RewriteCond %{HTTP_HOST} ^.*\.domain\.pl$ [NC]
RewriteRule . subdomain.php [QSA]

Redirect Multiple Subdomains to another domain name

Got a question about using mod_rewrite to redirect specific subdomains to use another domain. I am looking for the shortest possible way to do this without the need to create a separate rewrite rule for each of my domain names. I will be adding many new domain names (roughly 20-30 domains total).
So let's say my main domain name is example.com and I want to use that domain name for everything. So if any of my other domains are used, they will automatically be redirected to the main domain, preserving the subdomain prefix and the URL path.
Example:
test.example.org => test.example.com
test2.example.co.uk => test2.example.com
test3.example.net/hello/world.php => test3.example.com/hello/world.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.([a-zA-Z]+)\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1\.example.com/$1 [R=301,L]
I am unsure if the above will work correctly or if it is proper syntax. But basically, I would like to have it match the subdomain prefix, then any domain name, and any TLD (.org, .info, .biz, .co.uk, .net, etc.). I would assume it would need to make sure that it is not the correct main domain (example.com) first to prevent a infinite redirect loop.
Also, is there a possibility to check if HTTPS is ON or OFF and set the redirect correctly? If not, I can always have HTTPS set to ON.
Sorry for this confusion, although I want to make sure I get this right the first time without needing to program each and every one of the domains into the .htaccess.
Thanks! :)
(.*) is greedy so you don't want to use that, and I assume that you don't want to recreate an infinite loop.
RewriteEngine on
RewriteCond %{HTTPS}s ^..(s?)
RewriteRule ^ - [E=PROTO:http%1]
RewriteCond %{HTTP_HOST} !^\w+\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(\w+)\.\w+\..+ [NC]
RewriteRule ^/?(.*) %{ENV:PROTO}://%1.example.com/$1 [R=301,L]
That rule will not work because it will redirect any host ending with example.com back to example.com and you'll get a redirect loop. You need to change the regex to look like this, to match anything that doesn't end with .com:
RewriteCond %{HTTP_HOST} ^([^\.]+)\.?([a-zA-Z]+)\.(?:(?!com).)*$ [NC]

How do I hide actual directories using mod_rewrite?

I am hosting a couple of domains of the same wordpress installation, now I'd like to have a per-domain folder for some various files I need to put up there.
Essentially I want to map like this:
URL Path
webbfarbror.se/f/* _files/webbfarbror.se/*
grapefrukt.com/f/* _files/grapefrukt.com/*
This little snippet does the job nicely and the RewriteCond let's me enable and disable this on a per domain basis.
ReWriteCond %{HTTP_HOST} webbfarbror.se
ReWriteRule ^f/(.*)$ _files/%{HTTP_HOST}/$1 [L]
However, a file at say, http://grapefrukt.com/f/awesome.jpg is also accessible at it's "real" URL http://grapefrukt.com/_files/grapefrukt.com/awesome.jpg
All my attempts result in infinite redirects back and forth.
How do I disable access through the latter URL?
You can examine the original request as it was sent to the server, which is available as %{THE_REQUEST}. Checking for the /_files/ prefix indicates that the request was of the latter type, and you can then redirect to the appropriate format:
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/_files/
RewriteRule ^_files/[^/]+/(.*)$ http://%{HTTP_HOST}/f/$1 [R=301,L]