Forum Moderators: phranque
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 OnRewriteCond %{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
RewriteCond %{HTTP_HOST} !^www\.domain\.com?$
RewriteCond %{HTTP_HOST} ^[^.]+\.domain\.com$
RewriteRule ^(.+) %{HTTP_HOST}$1
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]
Jim
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?
Another thing is this line, what does this?:RewriteCond %{QUERY_STRING}!sub=[^&]+
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?
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
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]