Hide subfolder for subdomain - .htaccess

so my subdomain currently goes to the same root folder as my domain. I would like to accomplish the following:
sub.domain.com => sub.domain.com/sub/
with the /sub/ portion invisible to the user.
I do not want to have the following happen:
sub.domain.com => domain.com/sub/
Is there any way I can accomplish this without redirection and without getting stuck in an endless loop? I have this currently and I don't think it's working as it doesn't mask the /sub part:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/sub
RewriteRule .* http://sub.domain.com/sub[L]
I've looked endlessly online for something that accomplishes this but can't seem to find it...

I figured it out through a bunch of trial and error:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/sub
RewriteRule ^(.*)$ sub/$1 [L]

Related

Htaccess redirect subdomains to a specific page without changing the link

I am working on a php script on which i will generate links with random subdomains.For example : x.domain.com sius.domain.com x5-.domain.com and so on. In fact these subdomains doesn't really exist what i want is that when user goes to any link (RANDOM).domain.com it shows the content of domain.com/result.php?rand=(RANDOM).
PS:I considered (RANDOM) as the variable. and i want to exclude www.
I tried this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^ http://domain.com/result.php [L,R]
But nothing seems to work. Can anyone help ?
Try with:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteRule ^ /result.php?rand=%1 [L,QSA]
You can't redirect (http://... and [R]) without link change. With this code you rewrite without redirection.

stuck on htaccess rewrite url

i've been stuck on this for a few weeks, i think i'm close now, but i'm stuck on syntax for htcaccess..
here's what i'm trying to do: user types in site.com and is redirected to subdomain.wordpresshost.com; however the address bar still says site.com (i also want to keep anything after this point such as/blog.html etc)
nameserver is all set, now i'm just rewriting urls... heres the best code i have come up with.. it's just not working
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain.wordpresshost.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^site.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.site.com$ [NC]
RewriteRule ^(.*)$ http://www.site.com/$1 [L,R=301]
This code successfully changes subdomain.wordpresshost.com to site.com; however, it returns an error stating either 'server unavailable' or 'too many redirects'
I think i've hit my head against the keyboard so that I'm just making static noise, so i'd appreciate any help!
The last RewriteCond is unnecessary and is creating the redirection loop - www.site.com is rewritten to www.site.com… maybe you meant the non-www. Either way, it needs to be removed.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain.wordpresshost.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^site.com$ [NC]
RewriteRule ^(.*)$ http://www.site.com/$1 [L,R=301]

How to forward to new URL including all the variables?

I have been providing my subdomain name to friend until he manages to get new domain.
Now he has it and I would like to 301 redirect all the old links to his domain:
Example:
http://subdomain.my-domain.com/post.php?118&tg=602643
Redirect to
http://subdomain.com/post.php?118&tg=602643
So I want to keep up all the variables behind the post.php in the redirect
I am .htaccess newbie - can you please help me with providing the correct Rewrite rule?
Also, If you happen to have any good article about .htaccess and how to manage it, link is really appreciated.
Thanks
EDIT
Here are actual usecases I want to do:
redirect
http://raketa2.tasselhof.com/nastenka.php?115&up=648483
to
http://www.raketa2.cz/nastenka.php?115&up=648483
However, since there is no more subdomain existent on my site, i see the 404 error on my main page as this:
/nastenka.php?115&up=648483 -> Provided 404 Error
I tried:
RewriteCond %{Request_URI} ^/nastenka\.php [NC]
RewriteCond %{QUERY_STRING} ^\d+&tg=\d+$ [NC]
RewriteRule ^(.*)$ http://www.raketa2.cz/$1?%{QUERY_STRING} [R=301,L]
But with no good...
SOLVED
Duh! I am really dumb. I just added two new CNAME records to my domain DNS. Should do the trick
Do this:
RewrtiteEngine on
ReWriteBase /
RewriteCond %{HTTP_HOST} ^subdomain\.
RewriteRule ^(.*)$ http://subdomain.com/$1?%{QUERY_STRING} [R=301,L]
The QUERY_STRING post.php?118&tg=602643 will be present.
Assuming that there is no post.php on your domain,
Do this:
RewrtiteEngine on
ReWriteBase /
RewriteCond %{Request_URI} ^/post\.php [NC]
RewriteRule ^(.*)$ http://subdomain.com/$1?%{QUERY_STRING} [R=301,L]
If post.php is present on your domain but, you do not use similar query strings,
Do this:
RewrtiteEngine on
ReWriteBase /
RewriteCond %{Request_URI} ^/post\.php [NC]
RFewriteCond %{QUERY_STRING} ^\d+&tg=\d+$ [NC]
RewriteRule ^(.*)$ http://subdomain.com/$1?%{QUERY_STRING} [R=301,L]

Multilanguage htaccess file (LTD)

I want to have a multi language site. Now, I have 2 domains. The first one is the main domain. That is website.nl. And i have a domain alias, website.org. So the 2 domains share the same public_html folder.
What I want is that:
website.nl will use the file /index.php/$1 and
website.org will use the file /gb/index.php/$1 (So when the url is website.org/test you will use the file /gb/index.php/test (No url redirect)
I found on another topic on stackoverflow the following:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} website.org
RewriteRule ^(.*)$ /gb/index.php [L]
RewriteRule ^(.*)$ /index.php/$1 [L]
But this htaccess file won't work. I will get a 500 error. That's all.
Can someone see what's going wrong?
Your rules are looping, otherwise the 2 rules will mess with each other and loop indefinitely (e.g. requesting /foo will result in /index.php/index.php/index.php/index.php... etc thus returning 500). You need to add some conditions to stop the looping. Try changing the conditions and rules to:
RewriteCond %{HTTP_HOST} website.org
RewriteCond %{REQUEST_URI} !^/gb/index.php
RewriteRule ^(.*)$ /gb/index.php/$1 [L]
RewriteCond %{REQUEST_URI} !^/gb/index.php
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.*)$ /index.php/$1 [L]

.htaccess rewrite to subfolders for subdomains and the main domain

There's tons of resources online about using .htaccess to rewrite your subdomains and if need be, also rewrite your main domain to a subfolder. I have found plenty answers and most of them are exactly the same. I have been tediously testing these methods and I have the same problem in all cases.
Consider the wanted result:
maindomain.com : rewrite to /public_html/mainsite/
sub.maindomain.com : rewrite to /public_html/sub/
The fastest/cleanest way i have considered is the following:
RewriteEngine On
# Rewrite the main domain
RewriteCond %{HTTP_HOST} !sub.maindomain.com
RewriteCond %{REQUEST_URI} !^/mainsite
RewriteRule ^(.*)$ /mainsite/$1 [L]
# Rewrite the sub domain
RewriteCond %{HTTP_HOST} sub.maindomain.com
RewriteCond %{REQUEST_URI} !^/sub
RewriteRule ^(.*)$ /sub/$1 [L]
This works well except for 1 annoying issue; The line
RewriteCond %{REQUEST_URI} !^/mainsite
Basically prevents a rewrite loop, but if you browse to maindomain.com/mainsite/ it rewrites to /public_html/mainsite/ instead of /public_html/mainsite/mainsite/ hoping to raise a 404 not found. If i remove that line, i get a 500 server error as it goes into a loop :S
The issue is, that any one of these domains needs freedom of creating folders etc. and would like to ensure that there is absolute freedom in the sub-sub folders people create :S
Please could someone help here?
RewriteEngine On
# Rewrite the main domain
RewriteCond %{HTTP_HOST} !sub.maindomain.com
RewriteCond %{REQUEST_URI} !^/mainsite/.*
RewriteRule ^(.*)$ /mainsite/$1 [L]
# Rewrite the sub domain
RewriteCond %{HTTP_HOST} sub.maindomain.com
RewriteCond %{REQUEST_URI} !^/sub/.*
RewriteRule ^(.*)$ /sub/$1 [L]
You need to make it so that it does not match any file within the /mainsite directory not just the root (/mainsite). I would think you would need to do the same to the sub domain also.
actually try this if you are still looking for an answer
RewriteEngine On
# Rewrite the main domain
RewriteCond %{HTTP_HOST} !sub.maindomain.com
RewriteCond %{REQUEST_URI} !^/mainsite/.*
RewriteRule ^/mainsite/(.*)$ /mainsite/$1 [L]
# Rewrite the sub domain
RewriteCond %{HTTP_HOST} sub.maindomain.com
RewriteCond %{REQUEST_URI} !^/sub/.*
RewriteRule ^/sub/(.*)$ /sub/$1 [L]`

Resources