Forum Moderators: phranque

Message Too Old, No Replies

3 things i want to do with .htaccess

2 out of 3 success, 1 failed :-(

         

epool86

5:50 am on Mar 9, 2010 (gmt 0)

10+ Year Member



3 things i want to do with .htaccess

before i deciding to post this new thread, i already browse entire forum, and finally i manage to solved 2 out of this problem

1. rewrite "anything.domain.com" to "www.domain.com/folder_1/index.php?uid=anything" (anything can be anything, u know what i mean) all of them will be rewrited to "www.domain.com/folder_1/index.php?uid=anything"

RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com$
RewriteRule ^(.*)$ folder_1/index.php?uid=%2 [NC,L]


status: success!

2. with exceptional, anything except "blog.domain.com", if "blog.domain.com" i want special rewrite to "www.domain.com/blog", anything else still remain to "www.domain.com/folder_1/index.php?uid=anything"

RewriteCond %{HTTP_HOST} ^(www\.)?(blog)\.domain\.com$
RewriteRule ^(.*)$ %2/ [NC,L]


status: success!

3. for "blog.domain.com" ONLY, i want anything behind it (blog.domain.com/anythingHere) to be rewrite to domain.com/blog/anythingHere. for example, "blog.domain.com/pages/01" to be rewrited to "www.domain.com/blog/pages/01"

status: failed!

i don't know how to solve the 3rd problem, i tried the following code but it resulting 500 internal server error :-(

RewriteCond %{HTTP_HOST} ^(www\.)?(blog)\.domain\.com$
RewriteRule ^(.*)/(.*)$ %2/$2 [NC,L]

g1smd

9:22 am on Mar 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Are you sure you want to rewrite "everything", including requests for images, CSS and JS files, robots.txt, etc?

epool86

11:47 pm on Mar 9, 2010 (gmt 0)

10+ Year Member



yes i am, only for "blog.domain.com" ONLY.

example:
"blog.domain.com/anythingHere/bla/bla/bla.abc" to be rewrited to "www.domain.com/blog/anythingHere/bla/bla/bla.abc"

yes, includes files (image, css, js, etc)

jdMorgan

6:28 am on Mar 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your 500 server error is likely reporting an 'infinite rewrite loop' -- Check your server error log file. If you don't know where it is, ask your host. If there is no error log, plan get a new host if you are planning to do a lot of work with scripts and mod_rewrite.

Unless I'm missing something, you only need two rules, since "AnythingHere" includes "BlankHere," so one rule that accepts "anything" or "nothing" (as opposed to requiring "something") will cover both cases.

The key here is that in .htaccess, unless you explicitly state otherwise, any rule's output will be rewritten again if it matches that (or any other rule's) pattern. Using the [L] flag --although it improves efficiency-- will not help this problem, because mod_rewrite in .htaccess behaves recursively.

So the following two rules should do what you described.

# Rewrite blog.domain.com/<AnythingHere> to /blog/<AnythingHere> if not already rewritten
RewriteCond %{HTTP_HOST} ^(www\.)?blog\.domain\.com$
RewriteCond $1 !^blog/
RewriteRule ^(.*)$ /blog/$1 [L]
#
# Rewrite anything-but-blog.domain.com to /folder_1/index.php?uid=anything
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com$
RewriteCond %2 !^blog$
RewriteRule ^(.*)$ folder_1/index.php?uid=%2 [L]

Note that it is incorrect to say "Rewrite anything.domain.com to www.domain.com/folder_1/index.php?uid=anything", because you are rewriting "inside the server" so there is no "domain" in a rewrite, only a filepath. Therefore, I changed that description to "Rewrite anything.domain.com to /folder_1/index.php?uid=anything" in the comments in the code above.

Because of the form of the second rule, I will assume you are checking the client-requested URL-path within your script(s), because you wouldn't be able to have more that a single page Web site with no images or anything else unless this were true.

There's another problem, though, and that is that you are accepting both www- and non-www versions of your hostnames. You should 'pick one' and 301-redirect all www- requests to non-www (or vice versa) -- Either with an additional RewriteRule preceding the two posted here, or from within your script(s).

Jim

epool86

7:30 am on Mar 10, 2010 (gmt 0)

10+ Year Member



thank you so much for the explaination,

however i found some problem with your script, i forgot to mention that "www" and "blankHere" should not include in "anythingHere", as i want exclude www/blankHere (both www or blankHere go to normal root www.example.com). so i modified your script to

# Rewrite blog.example.com/<AnythingHere> to /blog/<AnythingHere> if not already rewritten
RewriteCond %{HTTP_HOST} ^(www\.)?blog\.example\.com$
RewriteCond $1 !^blog/
RewriteRule ^(.*)$ /blog/$1 [L]
#
# Rewrite anything-but-blog.example.com to /folder_1/index.php?uid=anything
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com$
RewriteCond %2 !^(blog|www)$
RewriteRule ^(.*)$ folder_1/index.php?uid=%2 [L]


this to avoid www.example.com to be rewrited to folder_1/index.php?uid=www

i already try the following address, and everything should work fine now (unless u see any other possible issue?)

the following address will goes to blog/ directory

http://blog.example.com/
http://blog.example.com/page/01
http://blog.example.com/images/img_01.jpeg
http://www.blog.example.com/

the following address goes to folder_1/index.php?uid=user

http://epool86.example.com/
http://user2.example.com/
http://www.user2.example.com/

and the following address goes to normal root of example.com

http://example.com/
http://www.example.com/

everything should work fine now (unless u see any other possible issue?)
thanks again.

[edited by: jdMorgan at 1:36 am (utc) on Mar 11, 2010]
[edit reason] Please use example.com. It can never be owned. [/edit]