Forum Moderators: phranque

Message Too Old, No Replies

htaccess rewriteEngine problem - which one of my variables is wrong?

         

verb

10:23 am on Jun 17, 2005 (gmt 0)

10+ Year Member



i am trying to configure a rewrite rule that will redirect foo.bar.com to www.bar.com/okay/foo

i have attempted to alter the script below with no luck


############################
rewriteEngine On
# Rewrite <subdomain>.example.com/<path> to example.com/<subdomain>/<path>
#
# Skip rewrite if no hostname or if subdomain is www
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\. [NC]
# Extract (required) subdomain (%1), and first path element (%3), discard port number if present (%2)
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^([^.]+)\.example\.com(:80)?<>/([^/]*) [NC]
# Rewrite only when subdomain not equal to first path element (prevents mod_rewrite recursion)
RewriteCond %1<>%3!^(.*)<>\1$ [NC]
# Rewrite to /subdomain/path
RewriteRule ^(.*) /%1/$1 [L]

can any one help me please

thanks

[edited by: jdMorgan at 4:43 pm (utc) on June 17, 2005]
[edit reason] example.com [/edit]

jdMorgan

4:39 pm on Jun 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



verb,

The code above will only work if your server's OS support POSIX 1003.2 regular expressions. The simpler techniques discussed in this thread [webmasterworld.com] can be used if this is not the case.

Jim

verb

3:26 pm on Jun 18, 2005 (gmt 0)

10+ Year Member



i recieve the following error message:

[Sat Jun 18 11:19:19 2005] [error] File does not exist: /home//fst/var/www/html/timmy

from typing in : http://timmy.example.com

with the following code:

# Rewrite <subdomain>.example.com/<path> to example.com/<subdomain>/<path> 
# the '' must be removed for it to work
# Skip rewrite if no hostname or if subdomain is www
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
# Extract (required) subdomain (%1), and first path element (%3), discard port number if present (%2)
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^([^.]+)\.mysite\.com(:80)?<>/([^/]*) [NC]
# Rewrite only when subdomain not equal to first path element (prevents mod_rewrite recursion)
RewriteCond %1<>%3 !^(.*)<>\1$ [NC]
# Rewrite to /subdomain/path
RewriteRule ^(.*) /$1/%1 [L]

my phpinfo tells me "posix: Revision $Revision: 1.51.2.2 $ "

so that means my server support's POSIX 1003.2 right?

[edited by: jdMorgan at 6:13 pm (utc) on June 18, 2005]
[edit reason] Example.com [/edit]

jdMorgan

6:10 pm on Jun 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code you posted may be working, but you apparently have a configuration problem elsewhere. Note the double-slash following "home" in the reported failing filepath:

> File does not exist: /home//fst/var/www/html/timmy

I can't tell if the above path is otherwise correct or not. If you can identify the specific problem and provide details, then maybe we can help.

Jim

verb

10:34 pm on Jun 18, 2005 (gmt 0)

10+ Year Member



i do apologize for that. i must have hit the / key accidently.

it should be /home/fst/var/www/html/timmy

jdMorgan

11:01 pm on Jun 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's going to be up to you to debug this. Do you recognize that filepath as "good" or "bad"? -- I certainly don't know. That is the path the server ends up at as a result of the rewrite, so what's wrong with it?

Jim

verb

3:22 pm on Jun 19, 2005 (gmt 0)

10+ Year Member



thanks for the reply,
the problem with the path is that it should be /home/fst/var/www/html/okay/timmy and not /home/fst/var/www/html/timmy.

when i type in okay.bar.com/timmy it goes to /home/fst/var/www/html/okay/timmy, which is where i want it to go.

i just want to be able to get there by typing timmy.bar.com and not havve to type 'okay' at all
.

any assistance offered would be greatly appreciated.

jdMorgan

6:36 pm on Jun 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If "okay" is a fixed value, then it's just a matter of adding that to the substitution in the rule:

RewriteEngine on
# Rewrite <subdomain>.example.com/<path> to example.com/<subdomain>[b]/okay[/b]/<path>
#
# Skip rewrite if no hostname or if subdomain is www
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
# Extract (required) subdomain (%1), and first path element (%3), discard port number if present (%2)
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^([^.]+)\.example\.com(:80)?<>/([^/]*) [NC]
# Rewrite only when subdomain not equal to first path element (prevents mod_rewrite recursion)
RewriteCond %1<>%3 !^(.*)<>\1$ [NC]
# Rewrite to /subdomain/path
RewriteRule ^(.*) [b]/okay/[/b]%1/$1 [L]

Basic references for mod_rewrite and regular expressions are cited in our forum charter [webmasterworld.com].

Jim

verb

7:16 pm on Jun 19, 2005 (gmt 0)

10+ Year Member



this seems to work

RewriteCond %{HTTP_HOST}!^(www\.)?(www¦mail¦images)\.domain\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com
# ok rewrite now
RewriteRule ^(.*) http://www.domain.com/userz/%2/$1 [P,L]

thanks, now i will try your suggestion

verb

8:05 pm on Jun 19, 2005 (gmt 0)

10+ Year Member



in the above post, userz should be 'okay'

quick question:

how do i prevent the url from changing in the address bar?

currently, when i type timmy.bar.com - it goes to www.bar.com/okay/timmy
but the address bar will continue to show timmy.bar.com.

After clicking on a link though, it will show the full url.

how do i configure it so that the address bar always shows timmy.bar.com?

thanks