Forum Moderators: phranque

Message Too Old, No Replies

htaccess for subdomain in joomla web, HELP

how can i make virtual subdomain via htaccess on joomla?

         

wanttobe

12:06 pm on Sep 26, 2011 (gmt 0)

10+ Year Member



how can i make virtual subdomain via htaccess on joomla?
when i write
http://subdomain.domain.com/ on address bar, and in the background internally point to
http://www.domain.com/index.php?option=com_content&view=article&id=73&Itemid=267&toko=subdomain

and when i write
http://subdomain.domain.com/pid=3
in the background internally point to
http://www.domain.com/index.php?option=com_content&view=article&id=70&pid=3&Itemid=267

i have activated wildcard DNS on my server and also i have include some script on original .htaccess and there is my script:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [OR]
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule ^(.*)$ index.php [F,L]

# Uncomment following line if your webserver's URL
# is not directly related to physical file paths.

########## Begin - Joomla! core SEF Section
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]

#### subdomain alias
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain\.com
RewriteRule ^([^.*])?$ index.php?option=com_content&view=article&id=70&pid=1&toko=%2 [L,NC]

[edited by: wanttobe at 12:39 pm (utc) on Sep 26, 2011]

g1smd

12:29 pm on Sep 26, 2011 (gmt 0)

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



Before you start modifying code, make sure you have the latest version of the .htaccess file.

Your example code is several years old and quite inappropriate.

Your additional code will need to go before the default Joomla rewrites but after the default Joomla blocks and redirects.

wanttobe

12:38 pm on Sep 26, 2011 (gmt 0)

10+ Year Member



thanks a lot for your reply g1smd,
i don't know much about htaccess, i got my (std) htaccess default from joomla installation.

so you mean i must write the code like this?

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [OR]
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule ^(.*)$ index.php [F,L]

# Uncomment following line if your webserver's URL
# is not directly related to physical file paths.


#### subdomain alias
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.domain\.com
RewriteRule ^([^.*])?$ index.php?option=com_content&view=article&id=70&pid=1&toko=%2 [L,NC]



########## Begin - Joomla! core SEF Section
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]


and how can i have the latest version of the .htaccess file?

g1smd

1:05 pm on Sep 26, 2011 (gmt 0)

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



The htaccess file is distributed as htaccess.txt in the root folder.

Your code is from a very old version. You need to get the file fom the latest version of Joomla.

What is
^([^.*])?$
supposed to mean? What it actually says is "match requests that do not contain a period or asterisk" and "match requests that are blank" (i.e. are for root).

lucy24

4:07 pm on Sep 26, 2011 (gmt 0)

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



What it actually says is "match requests that do not contain a period or asterisk" and "match requests that are blank" (i.e. are for root).

Even better: It says "match requests that do not consist of a period or asterisk" (but may optionally consist of any other single character, though it would be hard to make it anything other than /) :)

RewriteRule ^([^.*])?$ index.php?option=com_content&view=article&id=70&pid=1&toko=%2 [L,NC]


Did you mean pid=$1 (using the text you captured in the first part of the Rule)? Where's id=70 coming from? Is the new query string intended to replace the old one, if any?

wanttobe

5:37 pm on Sep 26, 2011 (gmt 0)

10+ Year Member



Even better: It says "match requests that do not consist of a period or asterisk" (but may optionally consist of any other single character, though it would be hard to make it anything other than /) :)

sorry, i'm still newbie, and still don't know much about htaccess.


RewriteRule ^([^.*])?$ index.php?option=com_content&view=article&id=70&pid=1&toko=%2 [L,NC]


Did you mean pid=$1 (using the text you captured in the first part of the Rule)? Where's id=70 coming from? Is the new query string intended to replace the old one, if any?


index.php?option=com_content&view=article&id=70 is standard code from joomla.
option=com_content => that page is showing component from joomla,
view=article => at this case component is article.
id=70 => is article id

as for &pid=1&toko=%2 => is the code that i include for;
pid=1 => Product id from...(vendor)
toko=%2 => vendor that i want to change to subdomain

lucy24

9:00 pm on Sep 26, 2011 (gmt 0)

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



Well, the first thing you do is fine-tooth-comb your joomla files for the newest version of htaccess. If g1 says you don't have the latest version, believe him. He knows whereof he speaks.

You will also have to start learning Regular Expressions. It helps if you can practice in something innocuous like a text editor, where the worst thing you can do is, say, replace all occurrences of the letter "e" with a string of html. Been there. Done that.

^([^.*])?$ is vanilla RegEx, with nothing that is specific to .htaccess.

^ at the very beginning means "beginning of the whole thing"
$ at the very end means "end of the whole thing"

Anything between ^ and $ therefore means "the entire content of the request".

() can mean either "capture this part" or "treat this part as a unit" or both. If you are capturing it, it will be $1, $2 and so on-- up to 9 captures, counting from the open-parenthesis-- in the second part of your Rule.

? means "the preceding element is optional". If it comes right after something in parentheses or brackets, it means everything in the parentheses or brackets. Otherwise it means "the preceding single letter". For example, jpe?g means "either jpeg or jpg" and html? means "either html or htm".

Brackets mean "anything within this group of characters". Or, if the very first thing after the brackets is ^, "anything not within this group of characters. So [^.*] means "anything other than a dot or asterisk". If you want it to mean "one or more anythings", say [^.*]+

CAREFUL! Inside brackets, . and * have their ordinary, literal meaning. The same goes for ? and ! and + and a few others. Everywhere else, they have to be "escaped" with \ and so on if you mean the specific, literal character, such as a period \. Yes, the backslash itself has to be escaped \\

Outside of brackets, . means "any one character of any kind whatsoever" including letters, spaces, punctuation and anything else you can think of.

Outside of brackets, * means "any number of X, including none at all". As with ?, X can be either a single letter, or a group in brackets or parentheses.

wanttobe

3:42 am on Sep 27, 2011 (gmt 0)

10+ Year Member



wow, thanks a lot lucy
i'll try to read and learn from basic again ...
if you don't mind may i ask you where i can find basic tutorial with some example.

best regards

lucy24

4:20 am on Sep 27, 2011 (gmt 0)

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



You mean Regular Expressions? I learned by trial and error. But I wasn't kidding about the text editor. There isn't nearly as much risk of serious injury-- especially when you've got unlimited Undo on your side-- and time isn't an issue.

That is, you're not allowed to fill your htaccess with misplaced (.*) expressions, because all those nanoseconds add up. But in a one-time search-and-replace it takes longer to work out a perfect RegEx than to run an imperfect one. Except for, ahem, the ones that send the text editor into perpetual motion that would go on for several days if I let it.* But it is much easier to force-quit an application on your computer than to bring a server back to life.

I did spend a lot of time here:

[regular-expressions.info...]
and then at the neighbor page, "advanced syntax".

Oh, hey, there's a tutorial. Never tried it myself. The further along you go, the more dialect-specific it gets. No, I don't know why they call them "flavors". Regular Expressions are powerful tools but I would not call them tasty.


* I once globally changed the line endings in an 85-mb file (I didn't make it that size) from \r\n to \n. It really did take several days. But it shaved a lot off the filesize.

wanttobe

2:42 am on Sep 30, 2011 (gmt 0)

10+ Year Member



i try this ...

RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$ [NC]
RewriteCond %1 !^(www|ftp|mail)$ [NC]
RewriteRule ^.*$ index.php?option=com_content&view=article&id=73&Itemid=267&toko=%1 [R,L]


but still can't work :(

lucy24

3:31 am on Sep 30, 2011 (gmt 0)

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



Does it simply "not work" -- that is, nothing happens -- or do you get some kind of error message? I would have worried about the [^\.] because I assume you mean [^.]. A literal . does not need to be escaped inside brackets, and that would leave a \ by itself.

RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$ [NC] 
RewriteCond %1 !^(www|ftp|mail)$ [NC]


Uhm? g1? Are you allowed to do that? The %1 in that position, I mean. Whether it's permitted or not, I don't think you need to do it, so let's not make trouble.

I also kinda think that unless you've got something very weird going on, you don't even need to mention ftp and mail. (Do you actually have subdomains called "ftp.domain.com" and "mail.domain.com"? Is this a joomla thing?)

Note that you've left out one possibility:

www.subdomain.domain.com


unless your host has already redirected this form before it ever reaches your htaccess. (If so, you may as well let them do the same with non-www addresses. It potentially creates one more redirect, but it won't break the bank, and is one less thing to worry about.)

Setting aside the www.subdomain. potential problem, you've got

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


You don't need an ending anchor for the second Condition, because all you need to know is that it doesn't start with www.

RewriteRule ^.*$ index.php?option=com_content&view=article&id=73&Itemid=267&toko=%1 [R,L]


Nooooo! Get that [R] out of there! You're rewriting, not redirecting.

As written, the Rewrite applies to all requests for anything in a subdomain. Is that what you wanted? For starters, all you need is

RewriteRule .


meaning "any request for anything". You're not capturing the request, so you don't need anchors. At most .* if you've got human users with seriously antiquated browsers so there's a possibility of a null request.

For the target,

index.php?option=com_content&view=article&id=73&Itemid=267&toko=%1


means that all incoming subdomain requests will be written to index.php. The existing query string, if any, will be thrown away, to be replaced with option=com_content&view=article&id=73&Itemid=267&toko=%1 where %1 is the name of the subdomain. Is that what you intended?

Do you really need four separate items in the query string (option, view, id, Itemid), if they're always the same? Seems like it would be tidier to make up a single term -- for example "rewrite=yes" -- and then let the php deal with it.

wanttobe

3:54 am on Sep 30, 2011 (gmt 0)

10+ Year Member



ohh, thanks a lot lucy for fast reply :)

yes, this is joomla things ..
the error page is not something 404, 500, etc, but something like .. if i put server ip on my address bar, like this

http://173.247.244.100/

and i change the script to this

RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com [NC]
RewriteCond %1 !^www$ [NC]
RewriteRule ^(.*)$ [domain.com...] [L]


but still, if i go to http://subdomain.domain.com it still showing page same as http://173.247.244.100/

i have enable mod_rewrite from joomla configuration

but if i enabling Search Engine Friendly URLs from joomla configuration, the

http://domain.com/index.php?option=com_content&view=article&id=73&Itemid=267&toko=%1&tid=$1

will change to

http://domain.com/toko-user?tid=$1&toko=%1

?

wanttobe

6:33 am on Sep 30, 2011 (gmt 0)

10+ Year Member



btw, if i comment for line 2 like this

RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com [NC]
#RewriteCond %1 !^www$ [NC]
RewriteRule ^(.*)$ http://example.com/index.php?option=com_content&view=article&id=73&Itemid=267&toko=%1&tid=$1 [L]


when i go to http://www.example.com

the page redirected to

http://example.com/index.php?option=com_content&view=article&id=73&Itemid=267&toko=www&tid=

but when i go to [sub.example.com...]

the page just showing the same page if i go to

[173.247.244.100...]

[edited by: wanttobe at 7:15 am (utc) on Sep 30, 2011]

lucy24

6:55 am on Sep 30, 2011 (gmt 0)

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



Awwwk! You've got to use example.com in all your examples, or they will turn into clickable urls-- yes, even if "domain.com" doesn't exist-- and then nobody can see what you really typed. I realize this is tricky when you're asking about different subdomains, but you can fake it with

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

so long as everyone understand that "www." really means "something other than www". Um. Not sure I understand it. (There are other ways to bypass the url conversion, but you have to figure them out for yourself.)

g1smd

6:55 am on Sep 30, 2011 (gmt 0)

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



Yes, the
RewriteCond %1 <something>
format is legal, and a neat way of making the comparison. It uses the capture from the previous condition. You can use $1 in the same way too.


Nooooo! Get that [R] out of there! You're rewriting, not redirecting.
Take care with ambiguous wording. For clarity "The code you presented is for an external redirect, you actually need an internal rewrite here." which could be read as meaning the exact opposite of what you wrote.

As for "not working". This is never a useful report. The only answer can be "because it is broken".

The problem is the (.*) as well as the [R]. You certainly do not want the [R] there. However with it there all requests are redirected to a new URL, and that URL still matches the (.*) pattern so the request is redirected again. It still matches the pattern and is redirected again in an infinite loop.

Change that code from a redirect to a rewrite and that part of the problem will vanish. The (.*) is still a problem. Do you really want to rewrite requests for images, css files, javascript files, and for robots.txt to be handled by your PHP script?

Use example.com in this forum to stop the URL auto-linking process. See RFC 2606.

lucy24

7:03 am on Sep 30, 2011 (gmt 0)

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



However with it there all requests are redirected to a new URL, and that URL still matches the (.*) pattern so the request is redirected again. It still matches the pattern and is redirected again in an infinite loop.

But does it still meet the conditions? I thought he'd got rid of the subdomain, and that's what the RewriteCond was checking for.

g1smd

7:05 am on Sep 30, 2011 (gmt 0)

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



It's a moot point, because the code should not redirect the request. It should be a rewrite.

With the URLs autolinked the code is unreadable anyway.

wanttobe

7:41 am on Sep 30, 2011 (gmt 0)

10+ Year Member



wow, really fast support
i really appreciate that , thanks lucy and g1

i have change the url to
http://example.com/index.php?toko=%1&option=com_content&view=article&id=73&Itemid=267

and new htaccess like this

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule .* index.php [F]

########## Begin - Custom subdomain
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com [NC]
RewriteCond %1 !^www$ [NC]
RewriteRule ^(.*)$ http://example.com/index.php?toko=%1&option=com_content&view=article&id=73&Itemid=267 [L]
########## End - Custom subdomain

########## Begin - Joomla! core SEF Section
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} (/[^.]*|\.(php|html?|feed|pdf|raw))$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
########## End - Joomla! core SEF Section

but the result still same, you can see the result here
[wahagaya.multitoko.com...]
the same page as
[173.247.244.100...]

g1smd

8:38 am on Sep 30, 2011 (gmt 0)

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



The subdomain code is still coded as a redirect, a 302 redirect in fact. This should be a rewrite.

RewriteRule can be configured to do redirects or rewrites. The syntax for each is only slightly different.

wanttobe

6:59 am on Oct 3, 2011 (gmt 0)

10+ Year Member



thanks g1, yes i found that my script still coded ad redirect. and for now i still learning the basic "command", the different between redirect and rewrite :)

i hope i can do that

wanttobe

4:38 am on Oct 4, 2011 (gmt 0)

10+ Year Member



hi, g1 and lucy
i have check and check again, why my htaccess seem not working, and i found out that i need to edit httpd.conf and change ServerAlias www.example.com to ServerAlias *.example.com.

and now with the last htaccess file i put above, i can use subdomain.example.com

but as g1 wrote above that my htaccess file actually use a 302 redirect code, i still confuse with that, i mean i still confuse the different between redirect and rewrite :(

lucy24

6:08 am on Oct 4, 2011 (gmt 0)

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



Sorry, I've lost track of what your actual RewriteRule is. The most recent one I can find in this thread is

RewriteRule ^(.*)$ http://example.com/index.php?option=com_content&view=article&id=73&Itemid=267&toko=%1&tid=$1 [L]

That is unambiguously a Redirect, because it gives the complete domain name. And since you don't explicitly say R=301, it defaults to 302. (The same thing happens in mod_alias. It's not a mod_rewrite special case.)

Or is it this one?

RewriteRule ^.*$ index.php?option=com_content&view=article&id=73&Itemid=267&toko=%1 [R,L]

If you still have the [R] in place, it's still a Redirect. But, again, it's the default 302. If you took away the [R] it would magically change into a Rewrite.

Way back in your first post (September 26? I can't possibly remember that far back!) you talked about "in the background internally point to..." That implies you wanted a Rewrite, not a Redirect.

From the user's point of view, the difference is that in a Redirect the browser's address bar changes, while in a Rewrite it doesn't. From your point of view, the first and simplest difference is that the server "thinks" it is in one place while it is showing the page content from somewhere else. If that page content includes anything like images or style sheets with relative links, you can expect chaos.

There is only one kind of Rewrite. But there are two kinds* of Redirect, 301 (permanent) and 302 (temporary). Except in very unusual circumstances, you want 301. But if you don't say, the server will assume you mean 302.


* This would be a good time for g1 to sit on your hands and ponder the phrase "Too Much Information" or possibly "Less Is More" ;)

wanttobe

1:58 pm on Oct 5, 2011 (gmt 0)

10+ Year Member



thanks lucy for your reply,
ok, i'll resume my work until now.

1. i want to have virtual subdomain like subdomain.example.com

2. the true url that i want to rewrite is
http://example.com/index.php?option=com_content&view=article&id=73&Itemid=267&toko=subdomain

3. i have activated joomla SEF so i got new url, like
http://example.com/toko-user?toko=subdomain

4. i want htaccess to rewrite that url (no.3) to something like this
http: //subdomain.example.com

5. the code that i put in htaccess is

########## Begin - Custom subdomain
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com [NC]
RewriteCond %1 !^www$ [NC]
RewriteRule ^(.*)$ http://example.com/toko-user?toko=%1 [L]
########## End - Custom subdomain

and i write url in address bar like http: //subdomain.example.com
i got the page that i want, but the url in address bar have change to
http://example.com/toko-user?toko=subdomain (redirecting)

but if i change the script to
RewriteRule ^(.*)$ /toko-user?toko=%1 [L]

i got internal server error (500)

can you tell me what is wrong with my code?

best regards

lucy24

5:02 pm on Oct 5, 2011 (gmt 0)

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



but if i change the script to
RewriteRule ^(.*)$ /toko-user?toko=%1 [L]

i got internal server error (500)

Subdomains make me tired. Did you mean, keep the same Conditions but delete the hostname from the target? Apache will then reuse the existing hostname, which is not example.com, it's subdomain.example.com.

Note that mod_rewrite tries to guess whether you have specified a file-system path or a URL-path by checking to see if the first segment of the path exists at the root of the file-system. For example, if you specify a Substitution string of /www/file.html, then this will be treated as a URL-path unless a directory named www exists at the root or [typo for "of", I hope] your file-system, in which case it will be treated as a file-system path.

It is nice of mod_rewrite to "try to guess", but you can see what happens when it guesses wrong. Or can't guess at all. It is much safer to tell it.

I've got a nasty feeling you may need something with a [PT] but this is way out of my domain. Ha, ha.

:: looking vaguely around for g1 or someone like him ::