redirect domain to www on codeigniter - php

What would be the best way to redirect codeigniter website to www domain? The only way that comes to my mind is to use htaccess for this but can't figure out exactly how. This is the rewrite rule I'm using right now to remove index.php from the url. How should I add the redirection to www domain correctly?
RewriteEngine on
RewriteCond $1 !^(index\.php|images|assets|uploads|robots\.txt|favicon\.png|favicon\.ico)
RewriteRule ^(.*)$ index.php/$1 [L]

See this URL
.htaccess Redirect non-WWW to WWW preserving URI string
Or try it
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www.mysite.com [NC]
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
Another example
See this URL
Codeigniter redirects for a new domain
Try it
Try adding the following to the .htaccess file in the root directory of your www.xyz.com site.
RewriteEngine on
RewriteBase /
#redirect www.xyz.com/A/B/controller/function
#www.xyz.com/B/controller/function
RewriteCond %{HTTP_HOST} ^www\.xyz\.com$ [NC]
RewriteRule ^A/(B/[\w]+/[\w]+)$ /$1 [L,NC,R=301]
#redirect www.xyz.com/A/controller/function to
#www.abc.com/controller/function
RewriteCond %{HTTP_HOST} ^www\.xyz\.com$ [NC]
RewriteRule ^A/([\w]+/[\w]+)$ http://www.abc.com/$1 [L,NC,R=301]

Use a CNAME record in your DNS.
This saves an HTTP request so is the most efficient option.

Add another condition under that:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Related

.htaccess redirect a folder in subdomain to a folder in main domain

Here's what I need.
I need to redirect (301) all content of blog.example.com/sample/ to example.com/sample/ and I'd like to accomplish this using htaccess.
Here's what I tried, but it didn't work:
RewriteCond %{HTTP_HOST} ^blog\.example\.com/sample [NC]
RewriteRule ^(.*)$ https://www\.example\.com/sample/$1 [L,R=301]
Thanks for the help.
RewriteCond %{HTTP_HOST} ^blog\.example\.com/sample [NC]
Here having /sample is problematic because HTTP_HOST only matches host name part in a request so it will never match this condition.
You can use this rule instead:
RewriteCond %{HTTP_HOST} ^blog\.(example\.com)$ [NC]
RewriteRule ^sample/ https://www.%1%{REQUEST_URI} [L,R=301,NC,NE]

Force to www. with htaccess if the url-router is index.php [duplicate]

I have a website say http://www.example.com/ in the root of my website, I have added .htaccess file to redirect any request of http://example.com/ to http://www.example.com/
Recently I have created a new section "Videos" so the URL for videos is http://www.example.com/videos/ . In this video folder I have another htaccess file which is performing rewriting for video entries. When I am trying to access http://example.com/videos/ then its not redirecting me to http://www.example.com/videos/
I think .htacces is not inheriting the previous rules from the parent directory. Can anyone please tell me what can be the rule I can add in the .htaccess file of /videos/ folder so that any request for http://example.com/videos/ will be redirected to http://www.example.com/videos/ URL.
This is a more generic solution, because it can be used with any domain name without having to specify the specific domain name in each .htaccess:
# Redirect non-www to www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
The contrary is also possible (www to non-www):
# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
I think it may just be that your existing rule is too strict, and is not getting triggered in your subdirectory because of this. Something like this should work site-wide:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*) http://www.example.com/$1 [R=301]
This is Best Solutions to redirect non-www to www URL's using htaccess. using this code in htaccess file and check url.
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]
I recommend everyone to stick with the below method it will only redirect the main domain only not any other sub-directory or sub-domain in your host.
# Redirect non-www to www only for main domain
# recommended if you have any/some sub-domain(s)
RewriteEngine on
RewriteBase /
# Replace yoursite and .tld respectively
RewriteCond %{HTTP_HOST} ^yoursite\.tld$
# Replace yoursite.com
RewriteRule ^(.*) http://www.yoursite.com/$1 [R=301]
Use this method if you are really sure and I hope you will that you won't ever use sub-domain with your website i.e. subdomain.yoursite.com or whatever. Than you're welcome to use the below method.
"Saying Again make sure if you really want to use this method"
# Redirect all non-www to www including subdomain(s)
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
The below method will redirect all www url to non-www including your sub-domains although you cannot use www.subdirecty.yoursite.com it will prompt you an error with 500.
# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

redirect folder subdomain to subdomain.domain.com in htaccess

I have a domain name domain.com and some folders inside it. When i am accessing the folders inside this domain i can access the folder like this.
http://www.domain.com/folder
I want to convert this url with htaccess to
http://www.folder.domain.com
I would like to have similar urls for all folders so its a wildcard entry.
Please tell me how can it be done with htaccess.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule ^(.*)$ http://subdomains.domain.com/$1 [L,NC,QSA]
You can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.com [NC]
RewriteRule ^([^/]+)(/.*)?$ http://www.$1.domain.com$2 [R=301,L,NC]
RewriteCond %{HTTP_HOST} ^www\.(.+)\.domain\.com [NC]
RewriteRule ^(.*)$ /%1/$1 [L]
To redirect requests to www.example.com/blog to blog.example.com, try this :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^blog/(.*)$ http://blog.example.com/$1 [L,QSA,R=301]
RewriteCond %{HTTP_HOST} ^blog\.example\.com$
RewriteCond %{REQUEST_URI} !^blog/
RewriteRule ^(.*)$ /blog/$1 [L,QSA]
For other you have to set dynamic name and pass as parameter, for simple blog you can take help of this question.

htaccess in subdirectory for parked domain

Motive: What I am trying to do is use parked domain and redirect it to subdirectory. and in subdirectory I have a script that needs pretty url's and htaccess is the only thing I can use.
Code in main .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteBase /
RewriteRule ^info/(.+)$ contact.php?a=$1
RewriteRule ^list/(.+)/page/(.+)$ list.php?a=$1&p=$2 [L]
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteRule ^(.*)$ http://www.example.com/m/$1 [R=301]
Code in /m .htaceess
RewriteEngine On
RewriteBase /
RewriteRule ^how/(.+)$ how.php?a=$1
RewriteCond %{HTTP_HOST} ^(www.)?main-example.com$ [NC]
RewriteCond %{REQUEST_URI} ^/m/(.*)$
RewriteRule ^(.*)$ - [L,R=404]
What i want is to achieve when user visits www.example.com/m/how/love the script should treet a get variable of a something like www.example.com/m/how.php?a=love but url should
remain pretty.
What my current code does is unlimited redirects to /m/404.shtml giving 301 in firebug.

How do I redirect to different page depending on the HOSTNAME .htacces with a Rewrite Condition/Rule

I have a site: www.asdf.com and an alias of www.fdsa.com
How can I check the HOSTNAME in the .htaccess and redirect them to different pages?
I know it's something along the lines of this:
RewriteRule (%{HTTP_HOST})^(.*)$ http://www.gunslot.com/$2 [L,R=301]
Basically how do I do an equivalent of this in the .htaccess (pseudo code)
if(hostname == "asdf") redirect to asdf.com/hello.html
if(hostname == "fdsa") redirect to asdf.com/goodbye.html
Not 100% clear what your trying to achieve but this answer's what you have asked.
.htaccess
RewriteCond %{HTTP_HOST} ^asdf\.com
RewriteRule ^(.*)$ http://asdf.com/hello.html [NC,L]
RewriteCond %{HTTP_HOST} ^fdsa\.com
RewriteRule ^(.*)$ http://asdf.com/goodbye.htm [NC,L]
If you want to redirect any request to asdf domain to hello.html on asdf, and any request to fdsa to goodbye.html add the following to your .htaccess file in the root of your domain.
RewriteEngine On
RewriteBase /
#Redirect any asdf domain or subdomain
RewriteCond %{HTTP_HOST} ^(.+\.)?asdf\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/(hello|goodbye)\.html$ [NC]
RewriteRule . http://asdf.com/hello.html [L,R]
#Redirect any fdsa domain or subdomain
RewriteCond %{HTTP_HOST} ^(.+\.)?fdsa\.com$ [NC]
RewriteRule . http://asdf.com/goodbye.htm [L,R]

Categories