How to rewrite from sub-domain to folder by .htaccess?


#1

Hello everyone:

I wanna rewrite a sub domain to a folder like this :
sub.mydomain.com/a.html”(access url ) -> “mydomain.com/sub/a.html”(real url)
I tried servral way to write the .htaccess file,
but failed .
Somebody help me ?

thx!


#2
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/sub/
RewriteRule ^(.*)$ /sub/$1 [L]

#3

Thank u sir. But are you sure it’s correct ?
I tried this, not work.
And it even looks not right.
The access url which needs to rewrite is SUB DOMAIN,
but RewiteCond of your solution is path, NOT matching a part of domain ?
even %{REQUEST_URI}, not %{HTTP_HOST} ???


#5

Help! Somebody help me !


#6

I have tried hundred times in rencent days,
It looks like RewriteCond does’t match, even like this :

RewriteCond %{HTTP_HOST} ^.*$

How disprited it is


#8

Thank you! I will try soon and report.


#10

Unfortunately, these are all not work, inclue many of their possible transformations, similar forms .
( I tried in last hour totally, again ).
For supplementary, I am a program coder and I was properly skilled at Regular Expression.
This fvkcing trouble make me unwell, i spent too much time on this.
Moreover, I’ve add “[R=302]” on RewriteRule,
for intend to monit whether it taken effect on the browser’s address bar, even partial.

Now i’m thinking about to give up on 000wh,
even purchase a vps to deploy Tomcat that I usually using.


#12

hum……it’s so excellent as you said?
Tell me why there’s no moderator help me in these days, firstly?
Tell me i must make a low-level mistaken, a stupid fault.


#14

This one is kidding me, he got my site pswd and told me that he done the job,
but he do nothing actually.
Fortunately, i just sent him a test site.


#15

@kui I guess you need to clear browser cache and data(or you can even use incognito mode) after making the changes suggested in above posts.See if this helps!!!


#16

Thank you, in the browser’s net monite tool,
i saw a occurence that the redirecting was executing recursion.
So, i saw the uri “site/folder/folder/folder/folder/folder/…” in location bar.
At start, i consider the behivor of browser is caused by
the instruction which is send from web server, so i did’nt care it.
But later when i modifed the htaccass file to validating yr perspactive,
I found it’s true, content of the site/page is cached in browser,
but in the contemporary, i have been aware that
the recursion is occuring in the brwoser! Not server !
seeing the “302” entry append one by one in the browser inspector,
how wonder, amazing it’s, it’s so surprising!


#17

@akhilkumar332
1.Your guiding is very valuable, it’s worked out the confusing fog in my mind.
Now i knew the rope that effect way of the htaccess file,
and the reason of that I tried so lot of times those are all not work.
Thank you very much !
Though i waited several day for you, but it’s worthy.
now i feel it’s easy to work out now.


#18