How can I implement dynamic subdomains in codeigniter with .htaccess?
Make sure that subdomains are enabled on your site. When you enter test.yoursite.com it should take you to the welcome page of your site. If instead it gives DNS lookup error then it means subdomains is not enabled on your site.
To enable subdomains on your site add *.yoursite.com entry to the DNS Zone records.
Second insert the following code in your .htaccess file and replace yoursite appropriately.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#codeigniter specific rule
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#codeigniter specific rule
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#this rule removes www from the URL if its used
RewriteCond %{HTTP_HOST} ^www.
RewriteRule ^(.*)$ http://yoursite.com/$1 [R=301,L]
#the auth system URIs which don't have subdomains
RewriteCond %{HTTP_HOST} ^yoursite.
RewriteRule ^(signup|signin|forgot_password)/?$ index.php?/auth/$1 [L]
#this rule handles the subdomains
RewriteCond %{HTTP_HOST} ^([a-z0-9]+).yoursite.com
RewriteRule ^(.*)$ index.php?/public_site/%1/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
Add more rules, if necessary, for handling auth system or subdomains. I know I did for my site to get pretty URIs.
Important Note:
I found that by default the subdomain URIs work very well with codeigniter without any of the above rules. You can access all of your site with test.yoursite.com/# URI instead of www.yoursite.com/#. But the URIs are not pretty and there would be more than one way to access a URI.
So if you use the above rules it will give you pretty URIs and, more importantly, will give you unique URIs. So if you use the above rules you will only get one URI for the signup page and that is http://yoursite.com/signup.
this code is working for my site, you can move your site into your other domain or folder ( whatever you want ).
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|application/assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
</IfModule>
This rule handles the subdomains
RewriteCond %{HTTP_HOST} ^([a-z0-9]+).yoursite.com. should probably also include the hypen -. RewriteCond %{HTTP_HOST} ^([a-z0-9-]+).yoursite.com.
Related
Where my site is hosted, I'm using .htaccess and it has a condition to remove the www and direct to the main page without the www.
<ifModule mod_rewrite.c>
Header set X-Frame-Options DENY
RewriteEngine On
# Required to allow direct-linking of pages so they can be processed by Angular
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) index.html [L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule (.*) http://meusite.com.br [R=301,L]
</ifModule>
The problem is this, when someone accesses an internal page with www, it falls for this check and is directed to the home, example:
If someone accesses the link: http://www.meusite.com.br/conteudo/94-vai-criar-um-site-to-your-employee-said-you-can-noble
It will direct to http://meusite.com under the condition.
What I need, is that it is directed to the following link: http://meusite.com/content/94-vai-create-a-site-to-your-employee-behavior-which-cannot-can- -fine only by removing the www from the link.
Does anyone know if this check is possible in .htaccess?
EDIT:
.htaccess is not able to translate your titles from portugese to english.
You should do redirection to normal domain with full link, and then do internal redirection with your backend (i.e. php, ruby) to proper translated link.
Use following code before your redirection, so links with conteudo will be catched here and redirected properly using backreferences:
RewriteCond %{HTTP_HOST} ^www\..* [NC]
RewriteRule ^\/conteudo\/(.*)$ http://menusite.com.br/content/$1 [R=301,L]
Soluction:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
I have a codeigniter installation at example.com/ci.
I have a subdomain foo.example.com. The document root for the foo subdomain is set to be home/public_html/ci.
I'm using the following rule in .htaccess to send requests for foo.example.com to example.com/ci/city/foo.
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule (.*) http://example.com/ci/city/%1/$1 [L]
It all works like I want it to except that the address bar url changes from foo.example.com to example.com/ci/city/foo. I would like it to remain foo.example.com. There is no R=301 in the RewriteRule (used to be but I removed it). The .htaccess file is in the ci/ folder and the rule is above all the codeigniter stuff.
The redirect works perfectly and the url remains foo.example.com with (Jon Lin's answer)
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteCond %{REQUEST_URI} !/city/
RewriteRule (.*) /city/%1/$1 [L]
but the codeigniter default controller is called instead of the foo method in the city controller.
Any help is appreciated.
When your rewrite rule's target has an http://example.com in it, a 302 redirect is implicit regardless of whether an R flag is used or not. You need to provide the URI path based on the subdomain's document root, so I'm assuming you want something like:
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteCond %{REQUEST_URI} !/city/
RewriteRule (.*) /city/%1/$1 [L]
If the subdomain's document root is in the /ci/ directory.
The other option is to use the P flag to reverse proxy the request:
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule (.*) http://example.com/ci/city/%1/$1 [L,P]
Your mileage may vary with this (might need to finesse it to fit your server and conditions), but doing some testing on my Mac, here's what I had mild success with:
Directory Structure
public_html/
ci/
application/
system/
.htaccess
index.php
I'm assuming that you have other stuff in your root public_html directory. So I'm letting the .htaccess focus on the CodeIgniter-related stuff by leaving it in the ci dir.
.htaccess
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.ciwildsub\.dev [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/city/%1/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
It's fairly self explanatory, but the first block is your subdomain check. I didn't bother excluding www but you may want to (as I said, your mileage may vary). The second block is a standard CodeIgniter index.php removal.
These rules will only apply to sub.example.com or example.com/ci/ URLs, since as I said, I assume your root has stuff that shouldn't be disturbed by rewrites.
CodeIgniter Config
$config['uri_protocol'] = 'PATH_INFO';
Because of the way Apache handles a URL like example.com/index.php/controller/method, it bypasses the index.php and handles it like any other directory segment. Also, mod_rewrite doesn't necessarily stop at the [L] tag -- it stops processing the .htaccess at that point, passes through the RewriteRule, and then runs that URL through the .htaccess. Setting PATH_INFO helps make sure CodeIgniter pulls the current URI correctly, and our .htaccess doesn't get stuck in a validation loop.
I will note, though, that I'm not entirely happy with what I see in my RewriteLog output -- there has to be a way to optimize this further, I'm just not sure of it yet (I'm done tinkering with this for today!). Sorry if any of the explanation here is a little out of whack - I'm not a server admin or mod_rewrite expert, I've just had fun tinkering with this. If I manage to find a better solution, I'll be sure to update this.
Looks like the END flag would be perfect for situations like this (to prevent [L] loops), but it's only available in Apache 2.3.9+. The search continues.
I got it to work correctly using the following rewrite rule
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteCond %{REQUEST_URI} !/city/
RewriteRule (.*) /city/%1/$1 [L]
and by setting
$config['uri_protocol'] = 'ORIG_PATH_INFO';
in the codeigniter config file. Thanks for all the help.
This worked for me
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /file_path/to/subdomain
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
I need to do a 301 redirect on a single page. If I just wanted it to work from www.example.com, I would have:
redirect 301 /urisegment1/urisegment2/urisegment3 http://www.example.com/urisegment1/urisegment2/NEWURISEGMENT3
The problem is, users may access http://www.example.com/urisegment1/urisegment2/urisegment3 or sometimes https://www.example.com/urisegment1/urisegment2/urisegment3/ if they have been logged in and access SSL-protected pages. Also I have the site set up locally so I can access it through http://www.example.local/urisegment1/urisegment2/urisegment3
How can I get this to work for all cases?
(That's the end of the question, my current .htaccess file below in case it is relevant. Note it's mostly not my own work, it is drawn from various sources).
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
#Removes trailing slashes (prevents SEO duplicate content issues)
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.+)/$ $1 [L,R=301]
#forces www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
#force homepage to http from https
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteCond %{HTTPS} on
RewriteRule ^$ http://www.example.com/ [R=301,L]
#If a controller can't be found - then issue a 404 error from PHP
ErrorDocument 404 /404.html
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^sys.*
RewriteRule ^(.*)$ /index.php/$1 [L]
# 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. The first condition allows access to assets, css, js and the robots file
RewriteCond $1 !^(index\.php|assets|css|js|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.example.com/$1 [R=301,L]
# Removes access to the system folder by users.
# Additionally this will allow you to create a System.php controller,
# previously this would not have been possible.
# 'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php
</IfModule>
I just tried the following rule
RewriteCond %{REQUEST_URI} !newvalue/?$
RewriteRule ^(.*)/[^/]+/?$ /$1/newvalue [L,R=301]
which should keep everything but the last URI segment, so it should work with http(s) and different domains.
You have to update the newvalue in both lines to avoid endless redirects.
I'am using a mod_rewrite to get the virtual subdomain as a variable in my site
and would like to add a rule to hide the index.php and all the variables in the url
but can't get it to work. Any suggestions please?
This is my actual .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.mysite\.com$
RewriteCond %{HTTP_HOST} ^(.*)\.mysite\.com$
RewriteRule ^$ /index.php?member=%1 [P,L]
Your best bet is probably to send in a support ticket and have the domain wildcarded.
.htaccess seems perfect.
Detail Description:
You will need to ask hosting company to add "wild-card" DNS set up to forward all subdomains to Web root directory. For example *.domain.com to www.domain.com. They can do this by adding A entry in DNS setting
HTACCESS Code
<IfModule mod_rewrite.c>
# Redirect to user blog (with any trailing path)
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9]).example.com(.*)$ [NC]
RewriteRule ^(.*)$ /index.php?member=$1 [R=301,L]
## Otherwise, force www;
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/ [R=301,L]
</IfModule>
I have a subdomain which is added as a serverAlias to the main domain. I am using this subdomain only for static content such as images and js, so I dont want the subdomain to follow rewrite rules and load index.php if a file is not found, I want it to show generic 404 page which is default from the server
I wanted not to apply the rule for my subdomain, this code worked for me
modifying bradym's block as example
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} !^static.example.com$ [NC]
RewriteRule (.*) - [R=404,L]
Something like this should work:
RewriteEngine On
RewriteCond %{HTTP_HOST} static.example.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) - [R=404,L]