htaccess ignore rewriterule for subdomain - .htaccess

here's my htaccess settings for redirecting urls which don't start with "www." (on my LIVE-environment):
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
i'm also running my mirrored testing environment on a subdomain called test.mydomain.com.
how can i disable this rule for it (as it would always redirect to the live environment)?
thanks

Just add test in your negative RewriteCond like this:
RewriteCond %{HTTP_HOST} !^(www|test)\.
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]

Anubhava's answer worked for me with a small adjustment:
RewriteCond %{HTTP_HOST} !^(www|test)\. [NC]
RewriteRule ^(.*)$ http://www.example.org/$1 [R=301,L]

Related

using .htaccess to add www prefix

I have a site hosted on godaddy. uses apache.
I used this code in .htaccess to add www prefix to the domain automatically
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/../$1 [R=301,L]
but instead of 'www.example.com' it goes to 'www.example.com/web'
I just want to convert 'example.com' to 'www.example.com'
If you just want to convert example.com to www.example.com then you just need to use:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=302,NC]
You can also lay it out like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE]
Make sure you clear your cache before testing this. You will notice I've just the flag R=302. This is a temporary redirect, use this while you're testing. If you're happy with the RewriteRule and everything is working, change these to R=301, which is a permanent redirect.
solved by using this
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule (.*)$ http://www.example.com/$1 [R=301]
RedirectMatch 301 ^/web/$ http://www.example.com/

How to redirect from folder to subdomain?

How to redirect with domain
site.com/image/some_symbols
to subdomain
some_symbols.site.com/
I've tried so
RewriteCond %{HTTP_HOST} ^(www\.|)site\.com$ [NC]
RewriteCond %{REQUEST_URI} ^image/(+*)$
RewriteRule ^(.*)$ http://%1.%{HTTP_HOST}/$ [R=301,L]
The %{REQUEST_URI} starts from the / at the beginning; which is why your pattern was not successful.
Better yet, do not rely on another RewriteCond, keeping it simply as:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?site\.com$
RewriteRule ^image/(.+)$ http://$1.%{HTTP_HOST}/ [R=301,L]

Redirect all non www to www except one subdomain with htaccess

I'd like to redirect all URLs not containing www to www
I used this:
RewriteCond %{HTTP_HOST} !^www.example.com$
RewriteRule ^(.*) http://www.example.com$1 [QSA,L,R=301]
Now I need one subdomain not to be redirect like this, or I get an error.
So I'd like to redirect any URL that does not begin with sub or with www to www.example.com
How can I do this ? thanks
You need a second RewriteCond. You can apply as many as you like to a RewriteRule.
Assuming anything that is not sub.mydomain.com needs to be www.mydomain.com, here is your code:
RewriteCond %{HTTP_HOST} !^sub.mydomain.com$
RewriteCond %{HTTP_HOST} !^www.mydomain.com$
RewriteRule ^(.*) http://www.mydomain.com/$1 [QSA,L,R=301]
But you can simplify this further using the pipe (|) character in Regex:
RewriteCond %{HTTP_HOST} !^(sub|www).mydomain.com$
RewriteRule ^(.*) http://www.mydomain.com/$1 [QSA,L,R=301]
RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This works for all domains, excluding any subdomains. have fun.
I tried the answer by Scott S but it didn't work, so I modified to use the one I was using for general 301:
RewriteCond %{HTTP_HOST} ^domain.com
RewriteCond %{HTTP_HOST} !^subdomain.domain.com
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]
And it works like a charm

.htaccess - how to force "www." in a generic way?

This will change domain.example to www.domain.example:
# Force the "www."
RewriteCond %{HTTP_HOST} !^www\.domain\.example$ [NC]
RewriteRule ^(.*)$ `http://www.domain.example/$1` [R=301,L]
How do I replace the "domain" part so that this works on any domain?
I would use this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The first condition checks whether the Host value is not empty (in case of HTTP/1.0); the second checks whether the the Host value does not begin with www.; the third checks for HTTPS (%{HTTPS} is either on or off, so %{HTTPS}s is either ons or offs and in case of ons the s is matched). The substitution part of RewriteRule then just merges the information parts to a full URL.
This will do it:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
If you want to redirect all non-www requests to your site to the www version, all you need to do is add the following code to your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
This won't work with subdomains.
domain.example correctly gets redirected to www.domain.example
but
images.domain.example gets redirected to www.images.domain.example
Instead of checking if the subdomain is "not www", check if there are two dots:
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ HTTP%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The following should prefix 'www' to any request that doesn't have one, and redirect the edited request to the new URI.
RewriteCond "%{HTTP_HOST}" "!^www\." [NC]
RewriteCond "%{HTTP_HOST}" "(.*)"
RewriteRule "(.*)" "http://www.%1$1" [R=301,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
This redirects example.com to www.example.com excluding subdomains.
This is an older question, and there are many different ways to do this. The most complete answer, IMHO, is found here: https://gist.github.com/vielhuber/f2c6bdd1ed9024023fe4 . (Pasting and formatting the code here didn't work for me)
this worked like magic for me
RewriteCond %{HTTP_HOST} ^sitename.example [NC]
RewriteRule ^(.*)$ https://www.sitename.example/$1 [L,R=301,NC]

Redirect the non-www URL to the www URL

I have referred all the related questions and tried the answer given, but it's not working for my site.
In my .htaccess file, I have written below code to redirect non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Are there any settings in Joomla that need to be set so it will use the .htaccess file?
This is what I use -
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
The differences are minor but should make it work. This works completely outside the scope of Joomla, you shouldn't have to do anything to Joomla for this to work.
You can put the following into your .htaccess to do this:
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
this works for me all the time

Resources