Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite issue

mod rewrite issue

         

b_cristian

11:52 am on May 13, 2008 (gmt 0)

10+ Year Member



I`m trying to make an mode rewrite and I encountered many problems.
It is about an mod rewrite that will generate subdomains like:

http://website.com/x.php?x=example into example.website.com

I`ll I have managed was this:

rewriteCond $1 !example\.php$
rewriteCond %{HTTP_HOST} ^(.*)\.website\.com [NC]
rewriteRule ^(.*) /example.php?post=$1 [L]

and the page should be like: http://someentryfromsql.website.com/someentryfromsql

I hope it is clear understanding.

The problem is that if I add more lines like if I replace example with example2 for instance I will get an 500 Internal Server Error wich is not good.

I hope I`ll get help here, someone to explain me were i`m doing wrong.

Regards,
Cristian.

[edited by: jdMorgan at 2:19 pm (utc) on May 13, 2008]
[edit reason] de-linked [/edit]

jdMorgan

2:18 pm on May 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your code does not agree with your description of your goal.

If you want to "map" subdomains to variable "x" and call example.php, then the rule is:


RewriteCond $1 !example\.php$
RewriteCond %{HTTP_HOST} ^([^]+\)\.website\.com [NC]
RewriteRule (.*) /example.php?x=[b]%1[/b] [L]

However, it is unclear what you want to do with the "page" that was requested from example.website.com/.

For example, if you request foo.website.com/bar.html, this rule will rewrite that request to /example.php?x=foo. The "bar.html" is lost.

I suggest you review the function that you are trying to achieve. You cannot write code until the goal is clear.

Jim

b_cristian

6:12 pm on May 13, 2008 (gmt 0)

10+ Year Member



I want that, example: website.com/x.php?y=z to point to z.website.com and still be able to run $_GET['z'] for php so that can I use it for the title, meta tags, h1, etc, if I use $1 instead of writing the paths manually.

jdMorgan

6:20 pm on May 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, well that is backwards.

z.website.com is a URL -- defined by the links on Web pages.

mod_rewrite can be used to point that URL to the existing server filepath /x.php?y=z

Web pages define URLs. Servers define directories and files.

That is all that mod_rewrite can do for you in this case.

Jim

b_cristian

12:33 pm on May 15, 2008 (gmt 0)

10+ Year Member



Lets say we have this code here:

RewriteCond $1 !x\.php$
RewriteCond %{HTTP_HOST} ^[^.]+\.y\.com [NC]
RewriteRule (.*) /x.php?post=$1 [L]

works, but if I add another lines:

rewriteCond $1 !z\.php$
rewriteCond %{HTTP_HOST} ^z\.y\.com [NC]
rewriteRule (.*) /z.php [L]

i get 500 internal error for the second page with "z".

and I don`t understand why I get this error.

jdMorgan

12:47 pm on May 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> and I don`t understand why I get this error.

Because with a "z.y.com" subdomain request, the RewriteCond HTTP_HOST pattern in the first rule will match the output of the second rule, leading to an 'infinite' internal redirection loop, rewriting back and forth between x.php and z.php.

Solution: exclude z.y.com from the first rule explicitly, either by subdomain (RewriteCond %{HTTP_HOST} !^z\.y\.com) or by url (RewriteCond $1 !^z\.php$)

Also, since the second rule is more specific (i.e. it only applies to one subdomain), best practice is to put it first.

When you get a 500-Server Error, your first stop should be the server error log. It will often tell you exactly what is wrong.

Jim