Welcome Guest, Not a member yet? Register   Sign In
Simple? .htaccess wildcard subdomain rewrite
#1

[eluser]Timothy_[/eluser]
Hello,

I know there is a lot of questions on here about this... but right now I feel like a deer stunned by oncoming headlights... I just don't know where to start and which option to choose.

My requirements are simple.

User goes to http://application.domain.com or http://www.application.domain.com and the real location of these files is http://www.domain.com/application

Obviously this needs to be done using wildcards so that any domain (even if it doesn't exist) triggers the rewrite.

I should also mention I have added the wildcard A record to my DNS entries in Cpanel *.domain.com

I hope this makes sense to someone and that someone can help me out.

Thanks,

Tim
#2

[eluser]InsiteFX[/eluser]
.htaccess

Keep www.
Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

# may need this one
RewriteRule (.*) http://www.example.com?/$1 [R=301,L]

Remove www.
Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^.*$ http://example.com%{REQUEST_URI} [R=301,L]

InsiteFX
#3

[eluser]Timothy_[/eluser]
Hello,

Thanks for replying. I had a look at your htaccess rules and even tried the first one, but unless I am very much mistaken these look like rules to simply remove or keep www. Although nice to know, my question really wasn't asking for this. Please correct me or read my original post again.

I have managed to find and edit the following htaccess which almost works.
Code:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.domain\.com$ [NC]
RewriteRule !^([a-z0-9-]+)($|/) /%2%{REQUEST_URI} [PT,L]

Now when visiting http://application.domain.com it is actually rewriting the content from http://domain.com/application

Perfect... until you try and visit any other file or folder in the website.

htaccess doesn't rewrite every url so we end up with http://application.domain.com/applicatio...r/function

All help would be appreciated.

Thanks,

Tim
#4

[eluser]Learn CodeIgniter[/eluser]
Code:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain.com [nc]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]
#5

[eluser]Timothy_[/eluser]
Hi,

Thanks for your reply but this is almost identical to what InsiteFX wrote and consequently is not an answer to my problems.

Maybe I am not understanding explaining this well. I'll try again.

When a user types in http://SUBDOMAIN.domain.com I want it to stay that way in the URL window.

However the real location of these files is http://domain.com/SUBDOMAIN

I know that this is possible using a *.domain.com DNS A record (which I have done) and a .htaccess rule.

Code:
RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.domain\.com$ [NC]
RewriteRule !^([a-z0-9-]+)($|/) /%2%{REQUEST_URI} [PT,L]

The above code rewrites the first request so when the user visits http://SUBDOMAIN.domain.com they are really browsing through http://domain.com/SUBDOMAIN.

This is great!

However even though I have set the BASE URL in codeigniter to http://SUBDOMAIN.domain.com, htaccess doesn't do anymore rewriting and we end up with the url http://SUBDOMAIN.domain.com/SUBDOMAIN/co...r/function.

Does this make sense to anyone else? I don't know how else to write it.

I am not trying to remove or keep the www. As far as I can see this an entirely different problem.

Thanks,

Tim
#6

[eluser]guidorossi[/eluser]
Today NETTUTS published a really good tutorial about something like this.
I think it's not exactly what you're looking for, but it may help...

Take a look:

http://net.tutsplus.com/tutorials/php/ba...deigniter/
#7

[eluser]Timothy_[/eluser]
Hi guidorossi,

Thanks for sharing that link.

Although that is indeed a very good solution I am pretty keen on keeping all my clients on separate CI installs rather than routing everyone through the one applicaiton. Therefore I would need to create a new virtual host entry in http.conf for every subdomain... not ideal and at the moment I dont even have access to http.conf.

I really think .htaccess is the way to go. It's almost working but it seems that once we start getting into virtual subdomains we are entering a niche territory.

Thanks anyway.

Tim
#8

[eluser]Timothy_[/eluser]
Hello Mods,

Is there any chance a moderator could please move this over to the Code and Application Development Forum? I think it might be a better fit?

Thanks,

Tim




Theme © iAndrew 2016 - Forum software by © MyBB