Htaccess rewrites for subdomains - .htaccess

I have been trying to put 2 seperate sites in subfoldres and use rewites to load them.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.udstde\.co\.uk
RewriteCond %{HTTP_HOST} test1\.udstde\.co\.uk
RewriteRule (.*) /test1/index.php [L]
RewriteCond %{HTTP_HOST} !^www\.udstde\.co\.uk
RewriteCond %{HTTP_HOST} test2\.udstde\.co\.uk
RewriteRule (.*) /test2/index.php [L]
The problem being the first one works fine but the second one doesn't.
Can anyone see where im going wrong?

I see the problem, in your first set of conditions,
RewriteCond %{HTTP_HOST} !^www\.udstde\.co\.uk
RewriteCond %{HTTP_HOST} test1\.udstde\.co\.uk
whenever test2 comes up, above condition turns true as both conditions turn false. (i guess)

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.udstde\.co\.uk
RewriteCond %{HTTP_HOST} ^([a-z0-9-]+).udstde\.co\.uk [NC]
RewriteRule (.*) %1/$1 [L]

Related

htaccess redirect all to www except IP

All my website page URL's are redirected to have a www at the beginning. So, if someone types http//xyz.com it will automatically make it http//www.xyz.com. This is fine for me. But the main problem is, if someone types 123.234.111.121 it will again put a www at the beginning, which is wrong. How do I fix it? Any help in this regard is highly appreciated.
My current setup is as follows
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
I even tried with this...
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^50\.56\.246\.162
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
But, it still doesn't work
You can use this rule:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^[0-9]+\.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
This condition RewriteCond %{HTTP_HOST} !^[0-9]+\. will skip this rule if you supply an IP address to access the website.
The above rule as suggested by anubhava is not working for me also. I google around and came with this answer.
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

.htaccess - affects all subdomains, how to exclude?

I have the following code:
RewriteEngine on
#Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^raal.co.il
RewriteRule (.*) http://www.raal.co.il/$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME} !\.(gif|jpg|JPG|png|css|php|js|html|htm|psd|rar|htc)$
RewriteRule ^(.*)$ index.php?a=$1 [L]
but then when i create subdomains, i get an error due to this code.
I want - fb.raal.co.il - to not be affected by the .htaccess of the main path.
i've tried adding something like this to the main .htaccess:
RewriteCond %{HTTP_HOST} ^fb\.raal\.co\.il
RewriteRule (.*) http://fb.raal.co.il/$1
but it wont work ..
just negate (!) that you dont want to redirect fb
RewriteCond %{HTTP_HOST} ^raal\.co\.il
RewriteCond %{HTTP_HOST} !^fb\.raal\.co\.il
RewriteRule (.*) http://www.raal.co.il/$1 [R=301,L]
You can use multiple conditions (RewriteCond) for just one RewriteRule
and use \. instead of .
RewriteCond %{HTTP_HOST} ^raal\.co\.il
dot is a one character in regex

mod_rewrite redirect of subdomain with subfolder combination

I have this code in my .htaccess to handle subdomains (http://foo.mydomain.com)
RewriteCond %{HTTP_HOST} !^www.mydomain.com
RewriteCond %{HTTP_HOST} ([^.]+).mydomain.com [NC]
RewriteRule ^(.*)$ filter.php?type=country&country=%1 [L]
The problem is what do i have to do if for example someone puts a link like this:
http://foo.mydomain.com/bar
i want to redirect this to another page different from the filter.php (say otherpage.php) from the previous code, i tried the next code but it isn't working
RewriteCond %{HTTP_HOST} !^www.mydomain.com
RewriteCond %{HTTP_HOST} ([^.]+).mydomain.com/([^/]+) [NC]
RewriteRule ^(.*)$ otherpage.php?type=country&country=%1 [L]
neither the next one:
RewriteCond %{HTTP_HOST} !^www.mydomain.com
RewriteCond %{HTTP_HOST} ([^.]+).mydomain.com [NC]
RewriteCond %{REQUEST_URI} ^/([^/]+)
RewriteRule ^(.*)$ otherpage.php?pname=$1 [L]
The link is always resolved with the first rule i wrote.
Add a condition to your first group that checks for an empty REQUEST_FILENAME. This will prevent it from matching when there's something other than a bare domain name.
RewriteCond %{HTTP_HOST} !^www.mydomain.com
RewriteCond %{HTTP_HOST} ([^.]+).mydomain.com [NC]
RewriteCond %{REQUEST_FILENAME} ^/?$
RewriteRule ^(.*)$ filter.php?type=country&country=%1 [L]

Why is this RewriteRule broken?

Ok folks, I'm at a loss.
RewriteCond %{HTTP_HOST} ^domain.nl$
RewriteRule ^(.*)$ poker/$1 [L]
Throws me a 500 error. If I remove the redirect and go to /poker/ manually it works. If I use this:
RewriteCond %{HTTP_HOST} ^domain.nl$
RewriteRule ^$ poker/ [L]
The front page is shown (but the css not, because obviously anything after the / is not redirected.
What could cause this (.*) to break?
Before this rule is just this to remove www:
RewriteCond %{HTTP_HOST} ^www\.([a-z-]+)\.([a-z]{2,3})
RewriteRule ^(.*)$ http://%1.%2/$1 [R=301,L]
After it is nothing that could interfere.
It was so easy.
RewriteCond %{HTTP_HOST} ^domain.nl$
RewriteRule ^(.*)$ poker/$1 [L]
Gives an infinite loop. I just had to add:
RewriteCond %{HTTP_HOST} ^domain.nl$
RewriteCond %{REQUEST_URI} !^/poker
RewriteRule ^(.*)$ poker/$1 [L]
(or as I just found out, creating an empty .htaccess with just RewriteEngine On in the /poker/ dir did the trick as well).

How would I redirect two domains using .htaccess

I'm trying to redirect one domain to the root and another to a directory. The problem I'm having is the second domain is overwriting the firsts redirection.
Here is what I have.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^http://example.site.net$ [NC]
RewriteCond %{REQUEST_URI} !^/.*$
RewriteRule ^(.*)$ / [L]
RewriteEngine On
RewriteCond %{HTTP_HOST} !^http://example2.com [NC]
RewriteCond %{HTTP_HOST} !^http://www.example2.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{REQUEST_URI} !^/example2_directory/
RewriteRule ^(.*)$ /example2_directory/$1
That's because 'example2.com' and 'www.example2.com' are not 'example.site.net', like your .htaccess rule states...
If you want those rules to apply to those specific domains, you need to remove the '!' in front of them. Otherwise you need to explain what specific domains need to point to what, not just 'one domain' and 'the other domain'.
Edit: Also, you're not supposed to include the 'http://' for your HTTP_HOST conditions.
I think you might not realize you're negating your comparisons with "!"
Also, you dont' have to turn the engine on twice.
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^http://example.site.net$ [NC]
RewriteCond %{REQUEST_URI} !^/.*$
RewriteRule ^(.*)$ / [L]
RewriteCond %{HTTP_HOST} ^http://example2.com [NC]
RewriteCond %{HTTP_HOST} ^http://www.example2.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{REQUEST_URI} !^/example2_directory/
RewriteRule ^(.*)$ /example2_directory/$1

Resources