Forum Moderators: phranque

Message Too Old, No Replies

i cant get it ! rewriting to a virtual subdomain with wildcard

         

Camaleon

4:52 am on Nov 20, 2008 (gmt 0)

10+ Year Member



Hi, iam using wildcard and it is working fine because any something.mydomain.com redirects to mydomain.com but i need to rewrite this:

www.mydomain.com/index.php?id_pro=111 to www.111.mydomain.com

or

www.mydomain.com/index.php?id_pro=something to www.something.mydomain.com

I think that both are the same, i tried several rules but i cant get it !

this was the last rule i try:

rewriteEngine on
rewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com [NC]
rewriteCond %{HTTP_HOST) !^www\.
rewriteRule .* index.php?id_pro=%1 [L]

Thanks alot.

jdMorgan

6:59 am on Nov 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> working fine because any something.mydomain.com redirects to mydomain.com

If you mean that when you type something.example.com into your browser address bar, that you get redirected and the address in your browser's address bar changes to example.com, then that is not "working fine" -- You must find that redirect and get rid of it before you can use subdomains of your main domain.

Note that we try to always use only the example.com domain in postings here, for many reasons.

I suspect that what you are trying to do here is to internally rewrite the requested URL www.<numbers>.example.com/ to the internal server filepath /index.php?id_pro=<numbers>

It is very important to state the "direction" of the rewrite properly, and to not mix up external redirects and internal rewrites.

However, none of this matters until you get your server to stop redirecting your subdomains to the main domain.

Jim

Camaleon

6:33 pm on Nov 20, 2008 (gmt 0)

10+ Year Member



hi, i solved the redirect issue, something.example.com it is not going to example.com anymore.

I suspect that what you are trying to do here is to internally rewrite the requested URL www.<numbers>.example.com/ to the internal server filepath /index.php?id_pro=<numbers>

in fact i will use letters, but actually iam using numbers for the testings, i think that the rules will be the same, correct me if iam wrong.

So, now what i should do ? add the subdomains ? and then ?

Thanks alot !

jdMorgan

7:08 pm on Nov 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The basic idea of your code looks OK. Cleaning up all the details:

RewriteEngine on
#
RewriteCond %{REQUEST_URI} !^index\.php$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteRule .* index.php?id_pro=%2 [L]

Now one major question remains: Your code and the code about will rewrite any request for *any* URL-path in a subdomain to the index.php script.

Are you sure that is what you want?

Do you really want to rewrite requests for images, external CSS and JavaScript files, robots.txt, sitemap.xml, labels.rdf, w3c/p3p.xml, and everything else to your one script?

If not, your rule will need to be modified to exclude these URLs from being rewritten, or to allow only the URLS you really want to rewrite to be rewritten.

Does the index.php script look at the Request_URI variable or use AcceptPathInfo to determine which "page" to generate? If not, you'll probably need to pass the requested page URL-path to the script as another GET variable.

Jim

[edited by: jdMorgan at 7:09 pm (utc) on Nov. 20, 2008]

Camaleon

7:38 pm on Nov 20, 2008 (gmt 0)

10+ Year Member



No, i dont want to rewrite requests for images, etc

I only need to send the GET variable in the subdomain, value.example.com = example.com/index.php?var=value

iam just focusing at SEO, so what should i change ?

Thanks morgan !

jdMorgan

11:06 pm on Nov 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



All of the answer depend on precisely *what* URLs you want delivered to the index.php script. You will have to figure that out.

You can do it two ways:
A list of URLs to be mapped to the script.
A list of URLs that must NOT be mapped to the script.

Look for common features of these URLs:
Do you want only the "home page" URLs sent tot he script?
Do they start with the same letters?
Do they all contain some sequence of characters?
Are they in the same directory?
Do they not have file extensions?

You use regular expressions to create a pattern to recognize these URLs, and then rewrite any URL that matches that pattern to the script. Or you can rewrite all URLs which DO NOT match the pattern to your script.

Jim

Camaleon

12:15 am on Nov 21, 2008 (gmt 0)

10+ Year Member



aja, so, in my case the best way to do it is a list of the urls mapped to the script because there are like 20 urls max, it wont be more, isent it ?

i mean:

value1.example.com = example.com/index.php?var=something1
value2.example.com = example.com/index.php?var=something2
...

The var value goes from the first valor to the twenty valor.

And there is another problem, the script in some cases will recibe another variable, for example:

example.com/index.php?var=something&Anothervar=some

i now how to rewrite a url to do this:

example.com/var/anothervar.htm

But there is something i dont know, how to do it work in this way:

var.example.com/anothervar.htm

Actually i even cant do it in this way whit only one var:

var.example.com

Iam lost, i dont know where i should start, so the better will be that you prentend i dont know nothing.

where i have to do first ?

create the 20 subdomains i suppose, and then ?

THanks alot Morgan, i apology for my english.

jdMorgan

12:31 am on Nov 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is my question:

What do you want to happen if someone requests this URL?
var.example.com/robots.txt

or this one:
var.example.com/images/logo.gif

Do you want to rewrite those URLs to your script?
Will your script properly handle those URLs?

Jim

Camaleon

1:58 am on Nov 21, 2008 (gmt 0)

10+ Year Member



i think that i would need to doit, in other case i wont be able to see my images, css and others, iam right ?

jdMorgan

1:19 pm on Nov 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is not a thing I can tell you.
You cannot guess the correct answer.
You will have to think about how your site and your script work, and test your URLs to find the correct answer.

Jim

Camaleon

6:41 pm on Nov 21, 2008 (gmt 0)

10+ Year Member



I tested, i create a subdomain = test.example.com

whit this rule:

RewriteEngine on
#
RewriteCond %{REQUEST_URI} !^index\.php$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteRule .* index.php?id_pro=%2 [L]

and i see only text, so i will have to rewrite the urls to the script, so now how i follow ?

thanks a lot

jdMorgan

7:36 pm on Nov 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know what "files" your users might request from their subdomains, so I cannot give you a complete answer.

You could try excluding images, multimedia, CSS and JavaScript files, and "system" files. You want to avoid rewriting a URL to your script if you know that your script cannot generate the correct response for that URL. I do not know all the details of your site, so the following is only an example:


RewriteEngine on
#
RewriteCond %{REQUEST_URI} !^/(index\.php¦robots\.txt¦sitemap\.xml¦labels\.rdf¦w3c/p3p\.xml)$
RewriteCond %{REQUEST_URI} !\.(gif¦jpg¦jpeg?¦png¦ico¦css¦js¦avi¦mpe?g¦wav¦wmv¦mp3¦swf¦flv)$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteRule .* index.php?id_pro=%2 [L]

It takes a lot of knowledge about how the Web site is built and how it functions to answer this kind of question, and the correct solution can be very, very complex.

Important: Replace the broken pipe "¦" characters in the code above with solid pipe characters before use: Posting on this forum modifies the pipe characters. The correct character is an unbroken vertical line, UTF-8 character-code 7C.

Jim

Camaleon

8:03 pm on Nov 22, 2008 (gmt 0)

10+ Year Member



Thanks ! it is working !

The only problem now is that i cant escape from the index script if i follow a link with another script, so i suppose that i need to add a new rule for a new script.

for example:

example.com/script.php?id_n=value&id_pro=value

RewriteCond %{REQUEST_URI} !^/(index\.php¦robots\.txt¦sitemap\.xml¦labels\.rdf¦w3c/p3p\.xml)$
RewriteCond %{REQUEST_URI} !\.(gif¦jpg¦jpeg?¦png¦ico¦css¦js¦avi¦mpe?g¦wav¦wmv¦mp3¦swf¦flv)$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com\([^.]+)
RewriteRule (.*)(.*) script.php?id_n=%2&id_pro=%2 [L]

Maybe something similar ? i dont have any idea how to do it work whit two variables.

Thanks

g1smd

2:13 am on Nov 23, 2008 (gmt 0)

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



This has an example with two parameters: [webmasterworld.com...]

Camaleon

7:15 pm on Nov 23, 2008 (gmt 0)

10+ Year Member



thanks alot g1smd, but i still dont have any idea of how to do it (i was not very clear in my last post), i have a variable at the beggining, and another one at the end of the domain.

var.example.com/another_var

Iam tryng to rewrite:

example.com/script.php?id_n=value&id_pro=value

into:

id_pro_value.example.com/id_n_value

Thanks alot

Camaleon

7:41 pm on Nov 25, 2008 (gmt 0)

10+ Year Member



any idea ?

Camaleon

9:32 pm on Nov 26, 2008 (gmt 0)

10+ Year Member



RewriteEngine on
#
RewriteCond %{REQUEST_URI} !^index\.php$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteRule .* index.php?id_pro=%2 [L]

In this rule the "%2" value what it means ? it is a reference to the third line i think but how it works, because if i change the value to a 3 it dosent work. example : "%3"

I need to understand that so i can make a new rule.

Thanks.

phranque

12:50 am on Nov 27, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], Camaleon!

it is a RewriteCond backreference to the 2nd grouped pattern in the preceding directives:
[httpd.apache.org...]

in your example, %1 would refer to the "(www\.)?" and %2 would refer to the "([^.]+)" and there is no %3 defined.

Camaleon

4:49 am on Nov 27, 2008 (gmt 0)

10+ Year Member



Great ! thanks phranque, so in case that i need to rewrite two vars using a subdomain i will have the following rule:

RewriteCond %{REQUEST_URI} !^/(index\.php¦robots\.txt¦sitemap\.xml¦labels\.rdf¦w3c/p3p\.xml)$
RewriteCond %{REQUEST_URI} !\.(gif¦jpg¦jpeg?¦png¦ico¦css¦js¦avi¦mpe?g¦wav¦wmv¦mp3¦swf¦flv)$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com\([^.]+).html
RewriteRule .* script.php?id_c=%3&id_pro=%2 [L]

So now using value_id_pro.example.com/value_id_c.html should it work, but iam doing something wrong on the rule, a sintax error maybe ?

Thanks alot.

phranque

5:52 am on Nov 27, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



that looks correct to me.
what is the requested resource in the server log file?

Camaleon

7:44 am on Nov 27, 2008 (gmt 0)

10+ Year Member



This ?

Not Found
The requested URL /8.html was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

Camaleon

8:25 pm on Nov 28, 2008 (gmt 0)

10+ Year Member



I tried a new rule:

RewriteCond %{REQUEST_URI} !^/(index\.php¦robots\.txt¦sitemap\.xml¦labels\.rdf¦w3c/p3p\.xml)$
RewriteCond %{REQUEST_URI} !\.(gif¦jpg¦jpeg?¦png¦ico¦css¦js¦avi¦mpe?g¦wav¦wmv¦mp3¦swf¦flv)$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com/\([^.]+).html
RewriteRule .* script.php?id_c=%3&id_pro=%2 [L]

I suppose that this one is the right, because it concerns:

value_id_pro.example.com"/"value_id_c.html

But still is not working, any ideas ?

phranque

4:34 am on Nov 29, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



The requested URL /8.html was not found on this server.

i don't see the "8" in your initially requested url anywhere - i wonder where that is coming from...

g1smd

10:10 am on Nov 29, 2008 (gmt 0)

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



I was wondering the same thing.

Camaleon

6:09 pm on Nov 29, 2008 (gmt 0)

10+ Year Member



Because iam trying this:

1.example.com/8.html = script.php?id_c=8&id_pro=1

iam testing the rules whit vars whit numbers for the moment.

Iam doing something wrong ?

Thanks alot.

phranque

12:33 am on Nov 30, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



i just noticed you are testing the requested uri, not just the domain, so this fails:
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com/\([^.]+).html
and therefore it never does the RewriteRule.

Camaleon

1:05 am on Nov 30, 2008 (gmt 0)

10+ Year Member



aaaa, let me se if i catched it, you are saying that whit that sintaxis rule iam only able to rewrite domains, i cant add a script value.

I mean that i will be able to rewrite links like this:

some.example.some.com some.example.net ...

But not like this:

some.example.com/some.html example.com/some.html ...

So, i will need a new rule to add ?

Thanks alot.

g1smd

1:12 am on Nov 30, 2008 (gmt 0)

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



No. What he said was that HTTP_HOST only looks at the domain name.

You test the filepath within the following Rule, or you can look at REQUEST_URI (or maybe THE_REQUEST) to get the path.

HTTP_HOST only holds the example.com part.

Camaleon

2:47 am on Nov 30, 2008 (gmt 0)

10+ Year Member



Ok ! now it is much clear.

Iam trying changuing the rule but i cant do it work, for example this:

RewriteEngine on
#
RewriteCond %{REQUEST_URI} !^/(index\.php¦my_script\.php¦robots\.txt¦sitemap\.xml¦labels\.rdf¦w3c/p3p\.xml)$
RewriteCond %{REQUEST_URI} !\.(gif¦jpg¦jpeg?¦png¦ico¦css¦js¦avi¦mpe?g¦wav¦wmv¦mp3¦swf¦flv)$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteRule (.*)/([^.]+).html$ my_script.php?id_c=$1&id_pro=%2 [L]

So i can do 1.example.com/8.html

Iam mixing some stuff in the last line, its probably that there is the error, but i dont have any idea of how to make the new rule, any clue ?

I saw some rules whit THE_REQUEST, but i cant figure it out, iam new at this.

Thanks.

phranque

12:09 pm on Nov 30, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



that is closer but you will not always have a slash in the pattern if this RewriteRule is in a .htaccess file.
in your example the url you are matching will be 8.html, not /8.html so the slash must be optional in the pattern.
This 36 message thread spans 2 pages: 36