Excluding a subdomain from .htaccess mod_rewrite rules? - php

I'm not too familiar with .htaccess files, and I'm trying to exclude a subdomain (something like dev.example.com) from the following rewrite rule that's already in place:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
This rule prohibits anyone from just entering example.com or http://example.com and forces the desired presentation of the URL, http://www.example.com.
I've tried a few options of excluding a subdomain from this rewrite rule, but to no avail. Each of the directories on the site have their own .htaccess file, but it seems this one is still taking precedence. Any ideas? Thanks!

The existing rule already excludes a subdomain. You just need to append a new condition:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^dev\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
You can also get rid of regular expressions:
RewriteEngine On
RewriteCond %{HTTP_HOST} !=www.example.com [NC]
RewriteCond %{HTTP_HOST} !=dev.example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
The syntax can be found at http://httpd.apache.org/docs/2.2/en/mod/mod_rewrite.html#rewritecond

I'm not sure if this could fit for you, but it is good practice to use apache virtualhosts configuration instead of .htaccess files. In that case, for the non-www -> www redirect, I usually use something like:
<VirtualHost *:80>
ServerName example.com
RedirectMatch permanent ^(.*) http://www.example.com$1
</VirtualHost>
That is generally safer than a mod_rewrite rule.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^(dev|www)\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

Related

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]

.htaccess rule for removing www from url

Whenever someone hits
http://www.example/consumer-complaints
it should become
http://example.com/consumer-complaints
The copy&paste .htaccess rules do not seem to work for me.
It removes www when someone hits
> http://www.example/consumer-complaints
but it redirects to only http://example.com
instead of
http://example.com/consumer-complaints
Can someone write an .htaccess rule for me
Thank you
The .htaccess Code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
Conversely, if you'd prefer to use "www" in all of your URLs, you can code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

.htaccess - sub domain always redirects to main domain

I don't have much experience with .htaccess rewrite rules so I'm hoping that someone can help.
I've created a subdomain for a website that I want to use for development work however I think that the current .htaccess file is causing a redirect to the main domain. This is the current .htaccess file:
<Files 403.shtml>
order allow,deny
allow from all
</Files>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F,L]
RewriteCond %{HTTP_HOST} !^www.domain.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^160\.153\.16\.12
RewriteRule (.*) http://www.domain.co.uk/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^domain\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.co\.uk$
The sub domain is dev.website.co.uk
Any advice will be very gratefully received.
Your .htaccess has rules which tell it to redirect to the main domain. Just remove them from your subdomain's .htaccess.
RewriteCond %{HTTP_HOST} !^www.domain.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^160\.153\.16\.12
RewriteRule (.*) http://www.domain.co.uk/$1 [R=301,L]
# I don't know what these are for
RewriteCond %{HTTP_HOST} ^domain\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.co\.uk$
I see many problems in your .htaccess, may be you have posted only part of it.
Main problem I see, the following lines catch any case where HTTP_HOST is different from www.domain.co.uk and redirect to www.domain.co.uk
RewriteCond %{HTTP_HOST} !^www.domain.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [L,R=301]
The following catch any case HTTP_HOST is different from ^160\.153\.16\.12 and redirect to www.domain.co.uk
RewriteCond %{HTTP_HOST} ^160\.153\.16\.12
RewriteRule (.*) http://www.domain.co.uk/$1 [R=301,L]
As far as I know, the remaining RewriteCond are useless without a corresponding rewrite rule.
RewriteCond %{HTTP_HOST} ^domain\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.co\.uk$
The RewriteCond directive defines a rule condition. One or more
RewriteCond can precede a RewriteRule directive. The following rule is
then only used if both the current state of the URI matches its
pattern, and if these conditions are met.
And at end, as #Marc B stated, you need to explain where the .htaccess is placed and if both the sites share the same root.

htaccess: redirecting site domain to subdomain

I need to redirect users accessing mysite.com to www.mysite.com in my .htaccess file.
The problem is anything I try won't work, I think because ^mysite.com doesn't exclude www.mysite.com so that will create a bad loop, but I usually get error 500.
I also need to exclude another subdomain, the img.mysite.com.
How can I do that?
I used many examples from the net & a few htaccess generators, which gave me this code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mywebsite.com[nc]
RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [r=301,nc]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This is what you need - taken from the default Drupal .htaccess
Try something like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.mywebsite.com$ [NC]
RewriteRule ^/(.)$ http://www.mywebsite.com/$1 [L,R=301]

htaccess question

Hay, In Plesk it gives me the ability to add a subdomain to a domain say x.y.com, the problem is that www.x.y.com gets treated as another subdomain. Is there a way to direct all traffic from www.x.y.com to x.y.com? I tried doing this in php using the 300 header, but stuff like www.x.y.com/mypage, doesn't get redirected. Could i use the .htaccess file to redirect all this traffic?
Thanks
# you probably want www.example.com to forward to example.com -- shorter URLs are sexier.
# no-www.org/faq.php?q=class_b
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>
Source: http://html5boilerplate.com
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^www.domain.com [nc]
rewriterule ^(.*)$ http://domain.com/$1 [r=301,nc]
From: http://www.webconfs.com/how-to-redirect-a-webpage.php
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.x.y.com [NC]
RewriteRule ^(.*)$ http://x.y.com/$1 [R=301,L]

Categories