Forum Moderators: phranque
lets say I have a subdomain like sub.domain.com I'd like to use my subdomain name as www.domain.com/index.php?param=sub
I have created all the subdomains from the apache server. And the rule i wrote down is something like this;
rewriteCond %{HTTP_HOST}!^www\.
rewriteCond %{HTTP_HOST} ^(.+)\.domain\.com
rewriteRule ^(.+)\.domain\.com$ /www.domain.com/index.php?param=$1
Thank in advance, sorry for my english :)
-s
I think you are pretty close...
You might try something like this:
rewriteCond %{HTTP_HOST}!^www\.
rewriteCond %{HTTP_HOST} ^(.+)\.domain\.com
rewriteRule ^(.*)$ http:// www.domain.com/index.php?param=%1 [P,L]
I believe you will need to pass the information as a proxy [P], since you are technically using a different domain.
$1 passes information from the RewriteRule, while %1 passes information from the condition. This way you will be passing the value of the subdomain name to the rule, from the condition. Then catching anything that qualifies as a non-www subdomain and rewrites it.
I also ran into something interesting the other day while working with conditions, and maybe someone who know more than I do can comment on it...
I was using a catch similar to yours, and passed the entire string EG subdomain.domain.com to the rule, consequently I had to switch from a relative catch all to ([^.]+), which catches everything up to the .(dot).
Hopefully this is somewhat helpful, and if not exact in its entirity should give you a direction to go in.
Justin
BTW Make sure spacing of http:// and! are correct.
I am using simple rewrite rules for most of my domains on the same server so i am quite sure the settings are OK. Do you have any idea?
#this is how i edited my .htaccess
RewriteEngine on
RewriteBase /
rewriteCond %{HTTP_HOST}!^www\.
rewriteCond %{HTTP_HOST} ^(.+)\.domain\.com
rewriteRule ^(.*)$ http:// www.domain.com/items.php?pf=%1 [L]
A couple of things:
1. Make sure there is no space between the http:// and the path.
2. Make sure there is a space between the } and the!.
3. Without the use of the proxy, the use of http generates an implied external redirect EG even without the [R] the browser will visibly forward to the file.
This is not a condition set I use often, I was mostly hoping to point you in a direction and give you some ideas of what may work... You might check the online documentation regarding the use of a proxy, etc. or wait for someone who does more of this type of redirection. I am guessing there is something simple I missed...
If you find the answer let me know, and if not I'll have a look around and see what I can find in a while...
Justin
PS Apache is about universal, no need to worry about your english here =).
I want my URL to be seen as sub.domain.com but when i click on sub.domain.com it comes as [domain.com...]
How can i keep the url as sub.domain.com?
-s
I don't regularly use a proxy, so am not as technical as I could be with their direct use, if you are using it and still having trouble, check the apache docs for the specific implementation...
Let me know how things go. (There may be something I am missing, but don't have time to think it through all the way right now.)
Justin
Welcome to WebmasterWorld!
If these subdomains are mapped to subdirectories of the same account on the same server, there should be no need to use the proxy function. Also, since the requested URL-path is not passed to the script, there is no need to parenthesize it in the RewriteRule:
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com
RewriteRule .* /index.php?param=%1 [L]
[edited by: jdMorgan at 10:01 pm (utc) on April 18, 2005]
Thank you
-serdar
little help please :)
The problem is what you asked for and what you are using it for are not exactly the same, and unfortunately rewriting is an exact science... The first rule set will work for what you requested. From there it get's a little more complicated, dependening on exactly what you need to do...
Thinking you are having a user click on a link to:
sub.domain.com/2/
And you want to rewrite that to the file from your example.
And your .htaccess file is in sub.domian.com/
You could use something like this:
RewriteEngine on
RewriteBase /
rewriteCond %{HTTP_HOST}!^www\.
rewriteCond %{HTTP_HOST} ^(.+)\.domain\.com
rewriteRule ^([0-9]+)/$ http:// www.domain.com/items.php?pf=%1&page=$1 [P,L]
This would work for the above example, but not:
sub.domain.com/page/2/
If you wanted to use this format and pass both the second param and variable you could use:
RewriteEngine on
RewriteBase /
rewriteCond %{HTTP_HOST}!^www\.
rewriteCond %{HTTP_HOST} ^(.+)\.domain\.com
rewriteRule ^([a-z]+)/([0-9]+)/$ http:// www.domain.com/items.php?pf=%1&$1=$2 [NC,P,L]
The above example would work for any /letter/number/ pattern... Hopefully this gives you some ideas of what you can/need to do. Keep in mind var's from conditions start with % and var's from rules begin with $.
Justin
Here is my current .htaccess
RewriteEngine on
RewriteBase /
rewriteCond %{HTTP_HOST}!^www\.
rewriteCond %{HTTP_HOST} ^(.+)\.domain\.com
rewriteRule ^([0-9]+)$ [domain.com...] [P,L]
Up to here everything works fine. However, when you click the button from the mainpage i'd like to send my URL as sub.domain.com and this time my modrewrite doesnt work with my current rules. Is there anyway to create two rules when there is an parameter do rule1 and if there is no parameter do rule2. do you guys have any idea? hope i did myself clear.
-s
Glad you got it working.
You can add as many rules/conditions as you need. Just be sure the condition(s) you are using for each are directly above the rule(s), end all rules with the [L] last flag, and don't write a loop for yourself.
Anyway it would look like this in your file (first is yours, works for (not)www., the second would then work for (is)www.):
RewriteEngine on
RewriteBase /
rewriteCond %{HTTP_HOST}!^www\.
rewriteCond %{HTTP_HOST} ^(.+)\.domain\.com
rewriteRule ^([0-9]+)$ [domain.com...] [P,L]
rewriteCond %{HTTP_HOST} ^www\.
rewriteCond %{HTTP_HOST} ^(.+)\.domain\.com
rewriteRule ^([0-9]+)$ [domain.com...] [P,L]
EXAMPLE ONLY, DO NOT USE.
As for the rule/condition set you will need to use to do this, I'm not exactly sure what you are asking for.
Hope this gives you an idea...
Justin
rewriteCond %{HTTP_HOST}!^www\.
rewriteCond %{HTTP_HOST} ^(.+)\.domain\.com
rewriteRule ^(page=)([0-9]+)$ [domain.com...] [P,R]
this rule works when i have a page parameter. However, for the first page i dont want to assign page parameter so the URL is going to be sub.domain.com in this case my rule is useless so i need to concat the rule above with the rule below in the same .htaccess file
rewriteCond %{HTTP_HOST}!^www\.
rewriteCond %{HTTP_HOST} ^(.+)\.domain\.com
rewriteRule ^(.*)$ [domain.com...] [P,R]
Hope this time it is clear :)
-s
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com
RewriteRule ^page=([0-9]+)$ /items.php?pf=%1&page=$1 [L]
RewriteCond %{THE_REQUEST} !\ /items\.php\?pf=.+
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com
RewriteRule .* /items.php?pf=%1 [L]