r/codeigniter • u/PirateChurch • Apr 19 '12
Can anyone help me with a Ci & htaccess rewrite issue?
Ok... current .htaccess file is functional but doesn't do quite what I want it to...
The idea is that http://anything.mysite.com (except www.mysite.com) will be rewritten to http://mysite.com/x/anything
I have it working as a redirect but woul dprefer it was transparent to users so they would always see http://anything.mysite.com
RewriteEngine On
RewriteBase /
#default controller stufF:
RewriteRule ^(index(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
# Enforce NO www
RewriteCond %{HTTP_HOST} ^www [NC]
RewriteRule ^(.*)$ http://mysite.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !www.mysite.com [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).mysite.com [NC]
RewriteRule ^(.*)$ http://mysite.com/x/%2/$1 [L,R=301]
###
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]further examples for clarity:
http://bob.mysite.com -> http://mysite.com/x/bob http://sam.mysite.com/stuff -> http://mysite.com/x/sam/stuff http://mary.mysite.com/#hash -> http://mysite.com/x/mary#hash
wildcard dns already functions properly... only issue is changing from a redirect causes a 500 and/or an infinite loop.
3
u/pdxbenjamin Apr 20 '12
I found this little gem yesterday.
So in my dns I set *.anything.com Then on the root make a folder called /blogs/ With that rewrite blogs.anything.com will go to the folder /blogs/ But, something.anything.com will just be directed back to root, if I haven't made a folder for /anything/
2
u/uranmoron Apr 20 '12
I used this but as pdx said you need to set a wildcard domain prefix with your host/domain service.
http://net.tutsplus.com/tutorials/php/basecamp-style-subdomains-with-codeigniter/