Mod_rewrite: How do I omit subdomains from rules? - .htaccess

Complete Apache newbie here. I'm trying to get my main URL to redirect to the www. Here's the code I'm using:
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]
The problem is, this has wrecked my subdomains. sub.domain.com goes to www.sub.domain.com, which doesn't work. So what do I write to fix that?

This might be enough:
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]
If all you're trying to do is make domain.com go to www.domain.com, then just use a RewriteCond that only matches domain.com at the start.

Related

Redirect to subdomain gives redirect loop error

This is my code, placed in my htaccess in the main directory of my domain. Trying to redirect country traffic (using mod_geoip) to my subdomain.
GeoIPEnable On
RewriteEngine On
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^US$ [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R,L]
However, I'm getting a redirect loop error when I put it into practice. When I switch my Rewrite rule to go to domain.com and then put the file in sub.domain.com it seems to work. What is wrong with my code? Thanks!
Thanks Anubhava for your help. I looked around a bit and found this modification that seemed to do the trick. The change was made to the RewriteRule.
GeoIPEnable On
RewriteEngine On
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^US$ [NC]
RewriteRule !^(subdomain) http://www.subdomain.domain.com [L,R]
I put this in my .htaccess and it worked! I saw this use with redirecting with a subfolder as well.
Try this rule with an additional condition to avoid redirect when host is already sub.domain.com:
GeoIPEnable On
RewriteEngine On
RewriteCond %{HTTP_HOST} !^sub\.domain\.com$ [NC]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^US$ [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R,L]

subdomain with www solve with htaccess

I have a problem with my subdomains. Normally every uses this kind of format: subdomain.domain.com and if this is entered there is no problem. When a user inputs www.subdomain.domain.com, the site doesn't work. I asked our hosting-support and they said I had to fix it with the .htaccess but I can't seem to find anything.
I found this but that is just for the domain and not for a subdomain:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.prosoccerdata\.com$ [NC]
RewriteRule ^(.*)$ http://prosoccerdata.com/$1 [R=301,L]
Many thanks!
This would take care of both main as well as sub-domains.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

Redirect with htaccess unless search

I'm not sure if this is possible, but here's whats going on.
I currently have an .htaccess rule to redirect example.com to store.example.com
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://store.example.com/[L,R=301]
This works great, however my search queries no longer work. What I'd like to do is redirect example.com to store.example.com unless I'm searching.
A sample search would look like this:
http://example.com/?s=my+search+query+string
Thank you in advanced for your help
Have you tried:
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{QUERY_STRING} ^!(s=(.*))$
Rewriterule ^(.*)$ http://store.example.com/ [L,R=301]

Non WWW to WWW redirection using htaccess File

I am using Codeigniter-.
My domain currently does not redirect to www version.
For example if I type mydomain.com then it stays mydomain.com. I want to redirect it to www.mydomain.com.
If someone types mydomain.com/controller/method then it should be www.mydomain.com/controller/method.
Another problem: I already tried other solutions but the problem is when it redirects to www version, it automatically adds "index.php" in the URL. But when I type www in the domain name then it works fine, no "index.php" in the URL. This problem occurs only during the redirection.
Here is my .htaccess file (I've removed the redirection code)
RewriteCond $1 !^(index\.php|system|rpc_relay.html|canvas.html|robots\.txt)
RewriteCond $1 !^(sitemap\.xml|export)
RewriteRule ^(.*)$ /index.php/$1 [L]
Any help would be greatly appreciated.
To redirect from http:// to http://www. , and also remove the route file (index.php) in the url, put these lines on your htaccess :
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond $1 !^(index\.php|images|css|js|styles|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
the domain is :
domain.com
folder with direct access :
images|css|js|styles
hope this help
I've used the following before:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
</IfModule>
To redirect domain.com to www.domain.com, you could use the following rewrite rule. Please replace domain.com with your own domain name.
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
I don't know why there are such complex RewriteRules answers, even though Gobhi has provided a nice generic solution (= whatever the domain name is, it works).
Here's my solution.
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
RewriteRule (.*)/index\.php$ $1/ [QSA]
</IfModule>

Htaccess to force use WWW for mutlidomain

I found this code to force using www.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]
But i have many domain that pointed to same directory. So i need a version of this code for multiple domains. It must run on any domain, is it possible ?
This should work:
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*) http://www.%{HTTP_HOST}/$1 [R=301]
It will redirect all request without a subdomain to www.domainame.tld.

Resources