homepage Welcome to WebmasterWorld Guest from 54.197.110.151
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Help with rewriting subdomains to subfolders
andre3

5+ Year Member



 
Msg#: 4037360 posted 11:59 pm on Dec 4, 2009 (gmt 0)

Hello Guys, I need some help here please, I am trying to rewrite

[domain.com...]

TO

[test.domain.com...]

An I would also love it to work fine if the user type:

[test.domain.com...] inside the browser.

When i type: www.test.domain.com in the browser, it goes to domain.com, instead of to the sub directories: /music/artiste

This is what I have:

rewriteCond $1 !^music/

rewriteCond %{HTTP_HOST} !^www\.domain+\.com
RewriteCond%{HTTP_HOST} !^www\.domain+\.com
RewriteCond%{HTTP_HOST} ^([^.]+)\.([^.]+)\.com

rewriteRule (.+) /music/%2/$1 [L]

Thank you

 

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4037360 posted 5:34 am on Dec 5, 2009 (gmt 0)

It sounds like you want something like this, although your example code doesn't seem to agree with the problem you described.

I will proceed on two assumptions: First, that your example subdomain "test" was meant to be the name of an artist, and second, that while you were initially testing without the leading "www.", the back-reference in the RewriteRule substitution was %1, not %2. I cannot explain or understand your test results otherwise.

This code rewrites client requests for URLs of the form www.<name>.example.com/<anything> and <name>.example.com/<anything> to the filepath /music/<name>/<anything>, while excluding requests for www.example.com URLs from being rewritten.

RewriteCond $1 !^music/
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteCond %2 !=www
RewriteRule ^(.*)$ /music/%2/$1 [L]

However, it's not a good idea to allow www.artist.example.com/page and artist.example.com/page to resolve to the same 'page' content. This creates what is known as 'duplicate content,' and that can mess up your search listings and rankings. You will have better results with search engines if you add a rule to detect requests for www.<artist>.example.com hostnames, and 301-redirect them to <artist>.example.com -- forcing removal of the "www."

Having done that, you can then rewrite only <artist>.example.com to your /music subdirectories and so avoid problems with duplicate content in search :

# Externally redirect requests for www.<artist>.example.com to <artist>.example.com
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com
RewriteRule ^(.*)$ http://%1.example.com/$1 [R=301,L]
#
# Internally rewrite requests for <artist>.example.com/<anything>
# to /music/<artist>/<anything>, excluding "www"
RewriteCond $1 !^music/
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteCond %1 !=www
RewriteRule ^(.*)$ /music/%1/$1 [L]

Please be careful with the format and syntax of mod_rewrite; Leaving out spaces is dangerous, and non-standard formatting makes your code harder for anyone else to read...

If the any of the above isn't clear, please refer to the resources cited in our Forum Charter.

[edit] Correction to third RewriteCond in first rule as noted below. [/edit]

Jim

[edited by: jdMorgan at 12:49 am (utc) on Dec. 6, 2009]

andre3

5+ Year Member



 
Msg#: 4037360 posted 12:24 am on Dec 6, 2009 (gmt 0)

RewriteCond $1 !^music/
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteCond %1 !=www
RewriteRule ^(.*)$ /music/%2/$1 [L]

This one does the trick, But if i type: http://www.example.com/ it rewrites to: music/www , instead of rewriting to the: main index.html on http://example.com/index.html

I appreciate your help Jim an everyone else.. thanks

[edited by: jdMorgan at 12:47 am (utc) on Dec. 6, 2009]
[edit reason] example.com [/edit]

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4037360 posted 12:47 am on Dec 6, 2009 (gmt 0)

You should use the two-rule version if you care about your search rankings.

If not, see the corrected code posted above. The correction applies only to the first-posted rule.

Jim

andre3

5+ Year Member



 
Msg#: 4037360 posted 7:10 pm on Dec 6, 2009 (gmt 0)

thank you. it works perfect jim

andre3

5+ Year Member



 
Msg#: 4037360 posted 10:16 pm on Dec 6, 2009 (gmt 0)

Guys, one more thing I discover. I was trying to help my self but it didn`t work out, if i type: test.domain.com/Artist_name without a / at the end. it redirects to: test.domain.com/music/artist_name/... however,, If i had a / at the end like this: test.domain.com/Artist_name/ it remains that way in the browser. Is it possible to automatically add the / to: test.domain.com/Artist_name if the user doesnt type any? to make the url remain looking like this: test.domain.com/Artist_name , instead of showing where I store the mp3. Thanks again

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4037360 posted 12:44 am on Dec 7, 2009 (gmt 0)

OK, so the idea is that "test.domain.com/Artist_name" should externally redirect to "test.domain.com/Artist_name/" and not to "test.domain.com/music/Artist_name" -- we don't want to 'expose' the /music subdirectory filepath as a URL. After the redirect, we will then internally rewrite "test.domain.com/Artist_name/" to "/music/Artist_name".

# Fix missing trailing slash, regardless of www preceding <artist> subdomain
# Externally redirect requests for www.<artist>.example.com/<something> or
# <artist>.example.com/<something> to <artist>.example.com/<something>/
# (Fix both incorrect www.<artist> hostname and add missing trailing slash)
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteCond %2 !=www
RewriteRule ^(.*[^/])$ http://%2.example.com/$1/ [R=301,L]
#
# Externally redirect requests for www.<artist>.example.com/<anything>
# to <artist>.example.com/<anything> (Fix hostname only)
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com
RewriteRule ^(.*)$ http://%1.example.com/$1 [R=301,L]
#
# Internally rewrite requests for <artist>.example.com/<anything>
# to /music/<artist>/<anything>, excluding "www"
RewriteCond $1 !^music/
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteCond %1 !=www
RewriteRule ^(.*)$ /music/%1/$1 [L]

Jim

andre3

5+ Year Member



 
Msg#: 4037360 posted 2:08 am on Dec 7, 2009 (gmt 0)

I dont know if I am doing something wrong, but i copied the code an edited it accordingly n its still the same here is wat i av in my htacces

RewriteCond $1 !^music/
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteCond %2 !=www
RewriteRule ^(.*[^/])$ [%2.example.com...] [R=301,L]

bear with me thanks

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4037360 posted 3:19 pm on Dec 7, 2009 (gmt 0)

The code I posted above is a complete replacement containing three rules with concise comments. If you choose not to test all three rules, then there is no point in my continued participation in this thread: Three rules are required to address your problem as described.

Remember to completely flush (delete) your browser cache before testing any new code on your server.

Jim

andre3

5+ Year Member



 
Msg#: 4037360 posted 1:35 am on Feb 3, 2010 (gmt 0)

Ok, I got it to work just tonight after I tried it again lol, one last thing, in this line: RewriteRule ^(.*[^/])$ [%2.example.com...] [R=301,L]


what if 'i have other domains that I might wana use along with example.com'

for example; if the user type: music.domain1.com/test.mp3 or he cud type: music.domain2.com/test.mp3 and get to the same file..

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4037360 posted 3:37 pm on Feb 3, 2010 (gmt 0)

Maybe something like:

RewriteCond %{HTTP_HOST} ^(www\.)?([^.:]+)\.([^.:]+(\.[^.:]{2,6}){1,2})(\.|\.?:[0-9]+)?$
RewriteCond %2 !=www
RewriteRule ^(.*[^/])$ http://%2.%3/$1/ [R=301,L]

You'll likely have to make similar changes to the other rules as well.

Jim

andre3

5+ Year Member



 
Msg#: 4037360 posted 3:04 am on Feb 5, 2010 (gmt 0)

yes works perfect, except if i try to download a uploaded mp3 file instead of coming out like ur downloading a reqular file it comes out like a text file with lots of funny codes in it, however when I remove

RewriteCond %{HTTP_HOST} ^(www\.)?([^.:]+)\.([^.:]+(\.[^.:]{2,6}){1,2})(\.|\.?:[0-9]+)?$
RewriteCond %2 !=www
RewriteRule ^(.*[^/])$ [%2.%3...] [R=301,L]

it downloads the file fine... thanks for your time

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4037360 posted 3:32 am on Feb 5, 2010 (gmt 0)

Yes well, as you can see, that rule adds a trailing slash to all requested URLs that don't have one, including "/abc.mp3"

I'm afraid it's up to you to thoroughly and completely define what you want to do before you start coding...

Perhaps you should not add trailing slashes if the requested URL-path contains a period in the final path-part. This means that no artist name may contain a period, though, or it will not be corrected if the trailing slash is missing.

Frankly, I think using a trailing slash on URLs which refer to 'files' is a mistake because normally, trailing slashes indicate directory request, but that's what you asked for...

Jim

andre3

5+ Year Member



 
Msg#: 4037360 posted 6:39 pm on Feb 5, 2010 (gmt 0)

I understand what I am coding, but I am just not good with htaccess rules, I thought there could be a rule to know web site pages and folders different from a media item (picture, music,, videos etc)

If that was the case, It would add slashes only to pages an folders but not media files...

Thanks you`ve been a great help to me..

andre3

5+ Year Member



 
Msg#: 4037360 posted 7:19 pm on Feb 5, 2010 (gmt 0)

Hey, is there a rule to say: If the url ends with a directory example: mp3.example.com/directory

then the rule will add the slash: mp3.example.com/directory/

If its not a directory it wont add any slash, possible to write with htaccess ?

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4037360 posted 11:45 pm on Feb 5, 2010 (gmt 0)

You can do a check using the
-d flag, but be aware that it needs to go check the physical disk in the server, and that slows things down a LOT.
andre3

5+ Year Member



 
Msg#: 4037360 posted 4:07 pm on Feb 6, 2010 (gmt 0)

could you givee an example please? or any other solution

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4037360 posted 8:55 pm on Feb 7, 2010 (gmt 0)

Did you look at the "-f" flag for RewriteCond in the mod_rewrite documentation at Apache.org? If so, what specific question(s) do you have about it?

If the nature of the last few responses seems baffling, a review of our Apache Forum Charter [webmasterworld.com] might be in order...

Thanks,
Jim

neostar



 
Msg#: 4037360 posted 4:38 am on Feb 15, 2010 (gmt 0)

yes it added trailing slashes at the end of requested uri if it is supposet to be dir or subdir.

[edited by: jdMorgan at 2:22 am (utc) on May 19, 2010]
[edit reason] TOS #28 [/edit]

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved