The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 18, 2019, 01:36:05 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: mod_rewrite & subdomains  (Read 2200 times)
arms
Expert
****
Offline Offline

Posts: 235



View Profile
« on: December 17, 2008, 11:24:23 AM »

so i added a subdomain on a shared host and it creates a folder that this suddomain points to.
i don't want this folder to be accessible as http://example.com/subdomain/ only as http://subdomain.example.com.

i have this piece of code that works via some fucking voodoo i don't understand to give a Gone error code but i'd prefer a 404.

Code:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.)?example.com$ [NC]
RewriteRule ^ - [NC,G]

help?

thanks
Logged
cd1
Rookie
**
Offline Offline

Posts: 16


View Profile
« Reply #1 on: December 17, 2008, 06:25:49 PM »

The RewriteCond line checks to see if the domain is NOT www.example.com or example.com... If it matches that rule, then it blocks access to it.

I'm kinda rusty in mod_rewrite, but there's a couple of things you could try.

Code:
RewriteRule ^ /404.php [NC]               #This will redirect to 404.php which is the script below to just return a 404 error.
or
RewriteRule   ^(.*)$   http://%1.example.com   [R=301,L]          #I believe this will do a 301 redirect from example.com/subdir to subdir.example.com
You may also need to take the '!' out of the rewritecond statement.  I think it might be matching the reverse of what you want to match.


404.php:
Code:
<?php
header
("HTTP/1.0 404 Not Found");
exit;
?>

Logged

No links in signatures please
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #2 on: December 17, 2008, 11:07:50 PM »

Another way to look at it is to see if the inbound URL is requesting the subDIR rather than the subDomain, and bounce accordingly. This little snippet assumes that you've not created a whole bunch of subdirs/subdomains, which would necessitate a little change. I think this is good, haven't tested it.

RewriteEngine on
RewriteCond %{REQUEST_URI} /subdomain/
RewriteRule /subdomain(.*)$ http://subdomain.domain.com$1 [L]
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
arms
Expert
****
Offline Offline

Posts: 235



View Profile
« Reply #3 on: December 18, 2008, 07:21:24 AM »

thanks guys.
i just used perks redirect and it works.
Logged
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!