Mask subdomain using .htaccess - apache

I want to mask my subdomain to a specific url on my main domain using .htaccess.
For example: https://sub.example.com to show the content of https://www.example.com/page/12345.
And the URL is still https://sub.example.com
I have got the following in .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.example\.com
RewriteRule ^https://sub\.example\.com https://www\.example\.com/page/12345
But if I go to https://sub.example.com, it shows content of https://www.example.com. Can't seem to figure out what's wrong. Please help!

Actually, you want to rewrite without redirecting. This requires enabling mod_proxy and mod_rewrite in Apache's httpd.conf.
Then, the rewrite should look like this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.example\.com$ [NC]
#only when there is query string and https on, if needed
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTPS} =on
RewriteRule ^$ https://www.example.com/page/12345? [R=301,NE,NC,L]
Reference:
What exactly does the Multiviews options in .htaccess?
htaccess rewrite without redirect
Apache: RewriteRule Flags

Related

.htaccess rewrite to redirect domain.com to domain.net/subdirectory

I would like to redirect somedomain.tv to anotherdomain.com/tv and (if possible) keep the URL as somedomain.tv
My .htaccess so far:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^radiouas\.tv$ [OR]
RewriteCond %{HTTP_HOST} ^www\.radiouas\.tv$
RewriteRule ^/?$ "http\:\/\/www\.radiouas\.org\/tv" [R=301,L]
</IfModule>
# This simply redirects but does not keep the original URL
I have found many ways to do that when you have only one domain (e.g. domain.com to domain.com/blog). But in my case, I'm using two different domains...
How can I achieve that? Thanks in advance.

.htaccess if subdomain show www.site.com/subs/. no redirect url must bu sub.site.com

I have problem with .htaccess.
I need:
if $_SERVER["HTTP_HOST"] is sub.site.com must show content from www.site.com/sub/. But URL must be like sub.site.com.
And if sub.site.com/content/ must show content from www.site.com/sub/content/
Is it Possible?
Setting subdomains from hosting not working for me because of my CMS.
Try adding these rules to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub.site.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/sub%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/sub%{REQUEST_URI} -d
RewriteRule ^(.*)$ /sub/$1 [L]
Enable 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
RewriteBase /
RewriteCond %{HTTP_HOST} ^sub\.site\.com$ [NC]
RewriteRule (?!^sub/)^(.*)$ /sub/$1 [L,NC]

.htaccess redirect certain strings

I have the following setup
go.me.com/123
I would like to redirect go.me.com/anything to me.com but have certain numbers redirect to certain pages.
Is that possible?
Sample Solution
Enable 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
RewriteBase /
# first specific rules for specific URIs
RewriteCond %{HTTP_HOST} ^go\.me\.com$ [NC]
RewriteRule ^10$ /sample1 [L,R=302]
RewriteCond %{HTTP_HOST} ^go\.me\.com$ [NC]
RewriteRule ^20$ /sample2 [L,R=302]
# now anything rule
RewriteCond %{HTTP_HOST} ^go\.me\.com$ [NC]
RewriteRule . / [L,R=302]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

Redirect subdomain to another subdomain

i want to know how could i redirect subdomain to another subdomain and also different domain name
for example
i want to redirect subdomain
forum.example.com
to
forum.example.net
thanks
This code should work for you. you need to add a redirect to your .htaccess file.
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.net/$1 [r=301,nc
Another good, but lazy way is to use the following link, to genereate the code for you htaccesss generator
Enable 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
RewriteBase /
RewriteCond %{HTTP_HOST} ^forum\.example\.com$ [NC]
RewriteRule ^ http://forum.example.net%{REQUEST_URI} [R,L]

Keeping the original subdomain url in the address bar

How can I change my rewrite rule to keep the original subdomain url in the address bar?
Currently if it type in "username.domain.com" it gets redirected to "domain.com/username"...and I would like the address bar to stay at "username.domain.com".
I'm using wildcard subdomains with this rewrite rule in my httpd.conf file:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteCond %{HTTP_HOST} !www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.com [NC]
RewriteRule . http://domain.com/sites/%2 [L]
</IfModule>
Is this even possible?
Thanks in advance.