Forum Moderators: phranque

Message Too Old, No Replies

Rewrite Rule

Using subdomain name as a variable for php file

         

Gelignite

6:49 pm on Apr 18, 2005 (gmt 0)

10+ Year Member



Hi everyone,
I am trying to create a rule for my website which needs to work like in the example below;

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

jd01

7:21 pm on Apr 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Gelinite,

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.

Gelignite

7:36 pm on Apr 18, 2005 (gmt 0)

10+ Year Member



Hi Justin,
Thanks much for your answer however i have received error it says "The server encountered an internal error or misconfiguration and was unable to complete your request"

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]

jd01

7:52 pm on Apr 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Gelignite,

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 =).

Gelignite

8:04 pm on Apr 18, 2005 (gmt 0)

10+ Year Member



My bad, I've just found out I need to copy the .htaccess file to the subdomain's folder as well :)

Thank you very much Justin. It works great now.

Serdar

Gelignite

8:07 pm on Apr 18, 2005 (gmt 0)

10+ Year Member



One more question, if you have time to answer me.

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

jd01

8:35 pm on Apr 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's where you'll have to serve the information using the proxy [P], without it, you are implying an external redirect, and will be visibly redirected to the http address in your rule...

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

jdMorgan

9:24 pm on Apr 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



s,

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]

Jim

[edited by: jdMorgan at 10:01 pm (utc) on April 18, 2005]

jd01

9:45 pm on Apr 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I defer to greater knowledge...

Thanks for stepping in Jim...

Justin

Gelignite

11:17 pm on Apr 18, 2005 (gmt 0)

10+ Year Member



Thank you guys,
my scripts all set :)

-s

Gelignite

3:35 pm on Apr 19, 2005 (gmt 0)

10+ Year Member



Hi, I am back :)
Here is the issue. Originally my link is [domain.com...] but the reason i used rewrite rules my URL is seen like brand.domain.com however I'd like to add another parameter to my brand. Script is working fine when there is not rewrite rule. [domain.com...] needs to be seen as brand.domain.com and i need to add page info to this line as well. Any suggestion would be great.

Thank you
-serdar

Gelignite

6:28 pm on Apr 19, 2005 (gmt 0)

10+ Year Member



RewriteEngine on
RewriteBase /
rewriteCond %{HTTP_HOST}!^www\.
rewriteCond %{HTTP_HOST} ^(.+)\.domain\.com
rewriteRule ^([^\.]+)(\.[^\.]+)+/\?(.+)$ [domain.com...] [P,L]

little help please :)

jd01

7:09 pm on Apr 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Gelignite,

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

Gelignite

4:27 pm on Apr 21, 2005 (gmt 0)

10+ Year Member



Sorry for the late answer, i got cold and was sleeping home for last couple days.

Actually this was the most clear example which made me to understand the whole idea of the .htaccess :)

Thank you very much

Serdar

Gelignite

5:41 pm on Apr 21, 2005 (gmt 0)

10+ Year Member



I am back :)

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

jd01

6:59 pm on Apr 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Gelignite,

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

Gelignite

7:04 pm on Apr 21, 2005 (gmt 0)

10+ Year Member



Actually this is not what I ask for :(

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

Gelignite

7:11 pm on Apr 21, 2005 (gmt 0)

10+ Year Member



lol i just put both htaccess into sameone and worked fine. thank much :)

jdMorgan

4:30 am on Apr 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I read over this thread quickly and may have missed something. But I see no need to invoke the proxy module just to prevent the rewritten URL from showing. The following should be much more efficient if it will work for you:

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]

Jim