redirecting ALL subdomains to custom page

Using a single web hosting account to host multiple sites

redirecting ALL subdomains to custom page

Postby javen » Sat Jan 27, 2007 5:29 pm

Hi
I'm new to mod_rewrite but do have an understanding of how it works.
I need to redirect all requests containing a subdomain to 1 page (i.e. customPage.php), but dont redirect requests that don't contain a subdomain.

examples:
sub.domain.com -> sub.domain.com/customPage.php
sub.domain.com/anyPage.php -> sub.domain.com/customPage.php
www.sub.domain.com/anyPage.php -> www.sub.domain.com/customPage.php

but DONT redirect requests not containing a subdomain, example:
domain.com -> domain.com
www.domain.com -> www.domain.com
www.domain.com/anyPage.php -> www.domain.com/anyPage.php
javen
 
Posts: 4
Joined: Sat Jan 27, 2007 4:42 pm

Postby richardk » Sun Jan 28, 2007 12:27 pm

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} !^(www\.)domain\.com$ [NC]
RewriteRule !^customPage\.php$ /customPage.php [L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby javen » Tue Jan 30, 2007 5:45 am

Hi Richard
Thanks for your help. Sorry for the delayed reply but I only just managed to get my Internet up and running again (moved house).

Your solution is along the lines of what I need but I need to make it a bit smarter. For example, at present your rewrite condition only ignores www.domain.com and not also domain.com. I think I need to make the 'www.' optional somehow - is that what brakets is supposed to do?

I also need to ignore any requested files that have *.css and *.js file extensions. At present any request for a style sheet or javascript file gets redirected to customPage.php.

Thanks for your help
javen
 
Posts: 4
Joined: Sat Jan 27, 2007 4:42 pm

Postby richardk » Tue Jan 30, 2007 11:09 am

It was missing a ? to make the www optional.

Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
RewriteRule !^(customPage\.php|.*\.(js|css))$ /customPage.php [NC,L]


You can add other extensions to ignore by adding them into this (js|css), separate them with |s.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby javen » Wed Jan 31, 2007 2:27 am

Thanks Richard, that did the trick.
The other thing I was looking to do was hide the .php file extension for security reasons.

I had a look at this posting here viewtopic.php?t=1829&highlight=hide+php+extension

Am I able to merge the stuff below with the rewrite conditions and rules I already have to achieve both?

RewriteCond %{REQUEST_URI} ^(.*[^/])/?$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule .+ %1.php [QSA,L]
javen
 
Posts: 4
Joined: Sat Jan 27, 2007 4:42 pm

Postby javen » Wed Jan 31, 2007 5:49 am

Doesn't matter, I put that code before the my previous rewrite conditions and rules and it seems to work perfectly. Cheers again for your help
javen
 
Posts: 4
Joined: Sat Jan 27, 2007 4:42 pm


Return to Domain Handling

Who is online

Users browsing this forum: No registered users and 95 guests

cron