Forum Moderators: phranque

Message Too Old, No Replies

subdomain htaccess problem

         

bahjaat

11:02 am on Apr 22, 2006 (gmt 0)

10+ Year Member



For a couple of days I am searching for a solution to my problem with the .htaccess file.

In my case I want to:

1. Rewrite [domain.com...] -> [domain.com...]
2. Rewrite [user.domain.com...] -> [domain.com?index.php?sub=user...]
3. Rewrite [user.domain.com...] -> [domain.com...]
4. Rewrite [user.domain.com...] -> [domain.com...]

This is my htaccess file at the moment:


Options -Multiviews
RewriteEngine On

RewriteCond %{HTTP_HOST}!^www\.domain\.com?$

RewriteCond %{HTTP_HOST} ^[^.]+\.domain\.com$
RewriteRule ^(.+) %{HTTP_HOST}$1

#subdomains
RewriteRule ^([^.]+)\.domain\.com\folder2 folder2.php?sub=$1&key=value
RewriteRule ^([^.]+)\.domain\.com\folder folder.php?sub=$1
RewriteRule ^([^.]+)\.domain\.com index.php?sub=$1

#www
RewriteRule ^folder$ folder.php

What's not working is:
- The query_string: &key=value
- rewriting user.domain.com/folder2 keeps rewriting this rule: RewriteRule ^([^.]+)\.domain\.com index.php?sub=$1

jdMorgan

1:56 pm on Apr 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The section with the first three lines of code does nothing -- it rewrites to the same hostname with the same filename:

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

It looks like a partial version of what is needed for the subdoamin rewrites, though. Delete those first three lines.

I will show a likely correction to your first subdomain rule only, and trust that you can work out the others.


#subdomains
RewriteCond %{QUERY_STRING} !sub=[^&]+
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com
RewriteRule ^folder2/?$ /folder2.php?sub=%1&key=value [L]

Note that RewriteConds only apply to the single following RewriteRule. Repeat them for the other subdomain rules.

Jim

bahjaat

2:01 pm on Apr 22, 2006 (gmt 0)

10+ Year Member



Thanks Jim,

I will give it a try. Will let you know.

Edwin

bahjaat

3:30 pm on Apr 22, 2006 (gmt 0)

10+ Year Member



This explained a lot to me:
Note that RewriteConds only apply to the single following RewriteRule. Repeat them for the other subdomain rules.

Another thing is this line, what does this?:

RewriteCond %{QUERY_STRING}!sub=[^&]+

Is it also possible to call: [sub.domain.com...] and catch that with a rewriterule?
I mean: no mather what is after the &. The rewriterule should leave it as is. What rule should that be?

jdMorgan

4:02 pm on Apr 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Another thing is this line, what does this?:
RewriteCond %{QUERY_STRING}!sub=[^&]+

That stops the rule from rewriting if the query string on the incoming URL already contains the "sub" or if it has already been rewritten. You can test without that line, but if you changed the RewriteRule, then the rule may then loop on itself, causing the page to fail to load and a server error.

Note that the modifications I made to the RewriteRule that follows should also prevent a loop, since it will now match only requests for "/folder2" or "/folder2/" and not for "/folder2.php" -- But I couldn't be sure exactly what you wanted there, based on your description. The query string test was therefore "a safety net."

Is it also possible to call: http://sub.domain.com/folder&action=submit and catch that with a rewriterule?
I mean: no mather what is after the &. The rewriterule should leave it as is. What rule should that be?

Not sure. It would be better if you would post a list of example URLs that you want to rewrite, and where you want to rewrite them to, and a list of URLs that you don't want to rewrite. Mod_rewrite coding usually isn't difficult, but defining the rewrites --defining the requirements-- is the hard part, because mod_rewrite and regular expressions require precision in definition of the problem and in coding.

It's also hard to write about this stuff. Just for example, I use notation like

Internally rewrite /folder<optional "/">?<original_query_string> to /index.php?<original_query_string>

or

Internally rewrite /folder/<subfolder!="private"><optional "/">?<original_query_string> to /new_<subfolder>index.php?<original_query_string>&folder=public

when I write specifications for rewrites.

If you post your code, a description of the desired action like I described, a list of URLs tested, the actual results, and any relevant entries from your server access logs and server error logs, that makes it much easier to get fast and accurate help.

Jim

bahjaat

4:41 pm on Apr 22, 2006 (gmt 0)

10+ Year Member



Jim,

Hope this will do the job.


RewriteCond %{QUERY_STRING} !sub=[^&]+
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^test/?$ /test.php?sub=%1 [L]

url: http://username.example.com/test
result: test.php&sub=username _ OK for me.

but

url: http://username.example.com/test&action=view
result: 404 page

apache log:
File does not exist: /home/renv/domains/example.com/public_html/test&action=view

I tried to write like you did:
internally rewrite <username>.example.com/folder?<original_query_string> to /folder.php?<orignal_query_string>&sub=<username>

[edited by: jdMorgan at 5:30 pm (utc) on April 22, 2006]
[edit reason] example.com [/edit]

jdMorgan

4:48 pm on Apr 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In order to keep the original query_string, rather than replacing it with a new one, you'll need to use the [QSA] -- Query String Append-- flag.

Change the rule to add this flag:


RewriteRule ^test/?$ /test.php?sub=%1 [QSA,L]

Jim

bahjaat

9:13 am on Apr 23, 2006 (gmt 0)

10+ Year Member



Thanks Jim,

This QSA thing killed me almost, but I am glad that it's working now!

Very very nice,
Edwin