Welcome Guest, Not a member yet? Register   Sign In
rewrite all subdomains to mainsite.com/controler/subdomain ?
#1

[eluser]shawndr[/eluser]
I'm no good with htaccess.

Would someone please help me accomplish this on a linux server?

anysubdomain.example.com
needs to rewrite to:
example.com/mycontroler/anysubdomain

Thank you
Shawn
#2

[eluser]Pascal Kriete[/eluser]
I do something similar on my own site using this:
Code:
RewriteCond %{HTTP_HOST} ^anysubdomain\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/(index.php) [NC]
RewriteRule ^(.*)$ /index.php/mycontroler/anysubdomain/$1 [L]

-Pascal
#3

[eluser]shawndr[/eluser]
Thanks for the sample, either I don't get it or I havn't expressed my self correctly.

Let me clarify,

I need to take the URL and,
1. "cut" the subdomain (whatever it may be) off of the front
2. append a constant controller "mycontroller"
3. append the subdomain on to the end of that.

so that: xxxx.mydomain.com becomes mydomain.com/mycontroller/xxxx

but I can't write a new condition line for every subdomain there are too many, and they are changeing.

Any thoughts on how to do that?

Thanks again
Shawn
#4

[eluser]Pascal Kriete[/eluser]
Ah ok. So it's more like having a subdirectory for each username.

The rewrite should look something like this (not tested):
Code:
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com.*$
RewriteRule (.*) http://example.com/mycontroller/%1/$1 [L]

You'll also need to make sure your dns and web server are set up so that ever requests ends up in the right spot.
#5

[eluser]shawndr[/eluser]
Thank you VERY much!

That is exactly what I was hopeing for.

Thank you
Shawn
#6

[eluser]Kumar Chetan sharma[/eluser]
Let me add more trouble to this.
I have 2 controllers, one Site, for plain html requests and one Ajax for Ajax calls. Initially the requirement was to send all requests for to be like following
http://www.site.com/sites/username/index.html or http://site.com/sites/username/index.html
and http://www.site.com/ajax/ajaxmethods or http://site.com/ajax/ajaxmethods
Easy!
The situation changed to following
http://www.site.com/username/index.html or http://site.com/username/index.html
and http://www.site.com/ajax/ajaxmethods or http://site.com/ajax/ajaxmethods
I some how managed this with following
Code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/CI/Ajax(.*)
    RewriteRule ^(.*)$ /CI/index.php/Ajax/$0 [L]    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /CI/index.php/Sites/$1 [L]

</IfModule>
CI is a folder in localhost/doc root of my web server.
But life is never simple and now the client has asked me to do this
http://www.username.site.com/index.html or http://username.com/index.html and I also have to maintain Ajax calls.
I know its easy but I am not able to solve this problem even after 16 hours :-(
Any pointer???
#7

[eluser]Phil Sturgeon[/eluser]
http://www.username.site.com/ makes him an idiot.

http://username.com/ how would that ever work? AJAX does not work cross-domain (unless you are using 3.5 and have some tricky header work in your code).
#8

[eluser]Kumar Chetan sharma[/eluser]
Hey Phil,
Hope you are doing good and have never heard or read following quote.
Quote:"It is impossible to make anything fool- proof because fools are so ingenious."
I managed to do this and am laughing at my self for my foolishness. Tongue
Code:
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{REQUEST_URI} ^/Ajax(.*)
RewriteRule ^(.*)$ /index.php/Ajax/%1 [L]


#RewriteCond %{REQUEST_URI} !^(www\.)?domain\.com$ [NC] - Avoiding this will handle in my application
RewriteCond %{REQUEST_URI} !^([a-b0-9]\.)?domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.domain\.com
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/Sites/%1/$1

#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ /index.php/Sites/$1 [QSA,L]
I have missed mod rewrite tag checking. So how do u check if it works?
On ur local windows machine look for hosts file in windows folder/system32/drivers/etc and add some entries for ur subdomain.domain.com
Create virtual host for www.domain.com and also add server alias *.domain.com to this virtual host entry.
place a print_r($_SERVER) in a php file and see how it works.
Regarding Ajax, all my Ajax calls go to a url like /Ajax/mymethods/parameters not to domain.com/Ajax/mymethods/parameters.
It seemed tough initially but I found a way to handle all requests that doesn't have subdomain present using php. Looks easy now :-)




Theme © iAndrew 2016 - Forum software by © MyBB