Forum Moderators: phranque
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]
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]
Jim
[edited by: jdMorgan at 2:58 am (utc) on Feb. 10, 2008]
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?
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]
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
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...
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]
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]
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]
RewriteCond $1 !poster\.php$
RewriteRule [b](.+)[/b] /poster.php?id=$1 [L]
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 :(