tags:

views:

16

answers:

2

I'm sorta a noob at these things but I'm trying to make a simple virtual subdomain with .htaccess. I have wildcard enabled and after lots of digging, this is what I've come up with:

rewriteEngine On 
rewriteCond %{HTTP_HOST} !^$ 
rewriteCond %{HTTP_HOST} !^(www\.)?khpedia\.com$ [NC] 
rewriteCond %{HTTP_HOST}<->%{REQUEST_URI} ^(www\.)?([^.]+).*<->/([^/]+) [NC] 
rewriteCond %2<->%3 !^(.*)<->\1$ [NC] 
rewriteRule ^(.+) /%2/$1 [L]

My directory is setup as

-root
--wiki
----index.php
--test

Right now when I travel to wiki.khpedia.com, I get a page not found. When I travel to wiki.khpedia.com/index.php, it travels to wiki.khpedia.com/wiki/index.php. I am somehow also able to access wiki.khpedia.com/test. If it doesnt seem obvious yet, I want to be able to go to wiki.khpedia.com/index.php and see wiki.khpedia.com/wiki/index.php but not in my address bar. Sorry for the text block and thanks for the help.

A: 
RewriteCond ^(.*)$ /wiki/$1 [L]

I'm confused about where subdomains come into your question. Could you give some examples of URLs you want to access in the browser and what they should point to at the server?

EDIT | Ah, I see now, Wrikken's answer handles the subdomains correctly :)

d11wtq
A: 

This works on the Apache side:

RewriteCond %{HTTP_HOST} !^www\.khpedia\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.+).khpedia\.com$  [NC] 
RewriteRule ^(.*) /%1/$1 [L,QSA]

But you're wiki software might make up it's own mind about redirects / urls.

Wrikken
hmm yes, this makes sense but I still dont think it is working for me.When I travel wiki.khpedia.com, I get an Internal Server Error. However, when I travel to test.khpedia.com/rewrite.php, I can see root/test/rewrite.php. But for some reason I can also travel to test.khpedia.com/test/rewrite.php and see the same file. Don't know if that matters, but thank you.
Changed a + to a * which should take care of "http://wiki.khpedia.com/" failing. We could try an elaborate rewritecond-fest (http_host, request_filename) to try to get a redirect the to 'non-dir' subdomain (http://wiki.a.a/wiki/index.php => http://wiki.a.a/index.php), but I think you're much easier of just doing a check for that in the .htaccess of the subdirectory itself.
Wrikken