Welcome to WebmasterWorld Guest from 23.22.46.195

Forum Moderators: Ocean10000 & incrediBILL & phranque

Mod rewrite in subdomain folder

Mod_rewrite newbie

   
2:43 am on Feb 10, 2008 (gmt 0)

5+ Year Member



Hi.

I have spent the last 11 or 12 hours trying to make mod_rewrite work in on my subdomain.

I wish to rewrite my URLs in a subdomain like this: http://subdomain.example.com/something --> http://subdomain.example.com/poster.php?id=something

My current subdomain .htaccess file:

php_value register_globals off
#
RewriteEngine On
RewriteBase /
#
RewriteCond %{HTTP_HOST}!^(www\.)?example\.com [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule ^(.*)$ /poster.php?id=$1 [L]

I have tried a lot of different things, but nomatter what it doesn't seem to work. What am I doing wrong here?

Thanks.

[edited by: jdMorgan at 2:59 am (utc) on Feb. 10, 2008]
[edit reason] de-linked example.com subdomains [/edit]

2:57 am on Feb 10, 2008 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Looks to me like you've set up an infinite rewriting loop. The symptoms of this would be that id would always end up as "id=index.php". But since you did not describe the mode of failure at all, I can't tell for sure.

Try this:


RewriteEngine on
#
RewriteCond $1 !poster\.php$
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.example\.com [NC]
RewriteRule (.*) /poster.php?id=$1 [L]

If this code resides in a separate filespace (e.g. a subdirectory) apart from the main domain's files, then the two HTTP_HOST RewriteConds are not necessary.

Jim

[edited by: jdMorgan at 2:58 am (utc) on Feb. 10, 2008]

3:34 am on Feb 10, 2008 (gmt 0)

5+ Year Member



Thanks, Jim.

But since you did not describe the mode of failure at all, I can't tell for sure.

The mode of failure was a 404 -- sorry for not mentioning that :)

I've tried your modified example and it works... Well, almost.

Almost because I get these differences when I test the .htaccess:

[subdomain.example.com...] = returns a 200/OK (the correct page and content).
[subdomain.example.com...] = returns a 404.

And when I type in the subdomain itself:

[subdomain.example.com...] = returns a 200/OK (the correct page and content).

But:

[subdomain.example.com...] = returns an empty 'poster.php' -- incorrect page and no content at all.

Strange isn't it -- do you have any idea why this is happening?

3:45 am on Feb 10, 2008 (gmt 0)

5+ Year Member



If this code resides in a separate filespace (e.g. a subdirectory) apart from the main domain's files, then the two HTTP_HOST RewriteConds are not necessary.

The .htaccess and the files do resides in a seperate parallel filespace:

/www/ = maindomain
/subdomain/ = subdomain

When I remove the HTTP_HOST RewriteConds then both of the 'http://subdomain.example.com/something' and the 'http://www.subdomain.example.com/something' works.

But neither of the 'http://subdomain.example.com/' or 'http://www.subdomain.example.com/' works - they return the empty 'poster.php'.

I whish for the non-www URLs to work -- the www URLs I would like to redirect to the non-www version of the URLs.

[edited by: OutdoorMan at 3:46 am (utc) on Feb. 10, 2008]

5:09 am on Feb 10, 2008 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



> But neither of the 'http://subdomain.example.com/' or 'http://www.subdomain.example.com/' works - they return the empty 'poster.php'.

Of course, because there is no subdirectory path with which to populate the id= parameter. You have to decide how you want to treat this case.

> I wish for the non-www URLs to work -- the www URLs I would like to redirect to the non-www version of the URLs.

That is a separate issue, best left until the first issue is resolved.

Jim

12:47 pm on Feb 10, 2008 (gmt 0)

5+ Year Member



Of course, because there is no subdirectory path with which to populate the id= parameter. You have to decide how you want to treat this case.

I don't understand this, Jim (I really don't know how to get further on this).

Is it so much different to mod_rewrite URLs in a subdomain compared to a maindomain -- if so, then what's the difference?

Maybe I'm missing the hole point here...

1:02 pm on Feb 10, 2008 (gmt 0)

5+ Year Member



Of course, because there is no subdirectory path with which to populate the id= parameter. You have to decide how you want to treat this case.

Maybe you misunderstod me or maybe I haven't explained myself properly.

The reason I mentioned 'http://subdomain.example.com/' is that it is the mainpage of my blog.

It is the blog posts I want to call by using: 'http://subdomain.example.com/something' -- and it works.

The 'http://subdomain.example.com/' and 'http://subdomain.example.com/something' is two different pages (physical pages).

That's why I dont understand that the 'http://subdomain.example.com/' shows an empty 'http://subdomain.example.com/something' (the physical page, not the url)

[edited by: OutdoorMan at 1:03 pm (utc) on Feb. 10, 2008]

2:03 pm on Feb 10, 2008 (gmt 0)

5+ Year Member



I think I've found a solution that works:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME}!-f
RewriteCond %{SCRIPT_FILENAME}!-d
RewriteCond %{HTTP_HOST}!^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.example\.com [NC]
RewriteRule ^([^/]+)/?$ /poster.php?id=$1 [QSA,L]

The above works as intended but there's still a problem with canonical issues:

'http://www.subdomain.example.com/something' returns a 404 (that's good)
'http://subdomain.example.com/something' returns a 200/OK (that's good)

'http://subdomain.example.com/' returns a 200/OK (this time it show the correct page - that's great!)

But unfortunately it also allows this: 'http://www.subdomain.example.com/' = 200/OK (this is bad)

Any suggestions on how to correct this?

[edited by: OutdoorMan at 2:04 pm (utc) on Feb. 10, 2008]

7:58 pm on Feb 10, 2008 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



If you don't want the "home page" to be rewritten, then it's a simple matter to require at least one character to follow the "/" in "example.com/" -- See the change to the rewriterule pattern below. Remember that everything depends on the RewriteCond and RewriteRule patterns, and they must be specific to any and all URL-paths that you wish to rewrite or redirect -- or to NOT rewrite or redirect.

Having fixed that pattern problem, you can then proceed to domain and subdomain canonicalization:

example.com/.htaccess:


# redirect example.com/<path> to www.example.com/<path>
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# redirect either www.<subdomain>.example.com/<path> or
# <subdomain>.www.example.com/<path> to <subdomain>.example.com/<path>
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com [OR]
RewriteCond %{HTTP_HOST} ^([^.]+)\.www\.example\.com
RewriteRule (.*) http://%1.example.com/$1 [R=301,L]


example.com/subdomain/.htaccess:

RewriteCond $1 !poster\.php$
RewriteRule [b](.+)[/b] /poster.php?id=$1 [L]

Using the -f and -d 'exists' checks is inefficient, and best avoided unless absolutely necessary. If you must use them, then try to place those checking RewriteConds as the final RewriteConds, so they are only executed when required. Remember that in order to do these checks, mod_rewrite has to call the OS to go check the filesystem. If the current state of the filesystem is not cached in memory, then each of those checks may require several physical disk access, and that one a sure way to slow down your users and beat your disk to death. :)

Jim

12:39 am on Feb 24, 2008 (gmt 0)

5+ Year Member



Sorry for the delay, Jim.

I have tried the example above but it don't work as intended. It seems as if the only thing that works (almost as intended) is this:

php_value register_globals off
#
Options +FollowSymLinks
RewriteEngine On
#RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
#RewriteCond %{HTTP_HOST} ^[^.]+\.example\.com [NC]
#
RewriteCond %{HTTP_HOST} !^subdomain\.example\.com$ [NC]
RewriteRule ^(.*)$ [subdomain.example.com...] [R=301,L]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /poster.php?id=$1 [QSA,L]

If I make any changes to the above (eg. remove the RewriteCond %{SCRIPT_FILENAME} !-f or RewriteCond %{SCRIPT_FILENAME} !-d or even make any changes to the "RewriteRule ^([^/]+)/?$") I see a view of the wrong page (when I view the sub domain front page it show the content of another page), some of the images are missing or else I get a 500 Internal Server Error -- I'm really lost here :(

 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month