.htaccess wrongly redirects to non-existent site - .htaccess

I have two domains. One of them contains a latin character, and is thus an IDN url. What I want is to redirect both the regular domain to the IDN domain, and to remove the www prefix from both domain names.
My .htaccess file looks like this:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^regularurl\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.regularurl\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.xn--idnurl-ujb\.com$ [NC]
RewriteRule ^(.*)$ http://xn--idnurl-ujb.com/$1 [R=301,L]
The problem is - it redirects correctly to xn--idnurl-ujb.com, but in any case (no matter if what url I redirect from - with or without www) appends www to the end, so that it becomes like this: xn--idnurl-ujb.com/www. This is clearly not what I intended, as that url is non-existent and redirects to the 404 site. I can't understand why this happens - I've tried many different code variations, too. I've successfully done some .htaccess redirects in the past, but they weren't IDN domain names - does that have anything to do with it? What am I doing wrong?
Also, if there is anything I could do to make the code easier or more dynamic, please let me know.

Related

How to redirect roodomain/addondomain urls to addondomain

My host does not know how to fix this.
I saw in google results URLs that worry me.
For example, I saw rootdomain/addondomain.com/url1.html etc
this happened because google bot was not redirected to addondomain.com/url1.html for example
So I want to redirect all URLs to addondomain.com only
Because this created duplicate content.
My root domain has nothing to do with addon domain...they have a completely different topic....
I already have redirection from addondomain.rootdomain.com to addon domain in htaccess....
but I want to add the new one too...
This is the code I already have
RewriteCond %{HTTP_HOST} ^addon\.root\.org$ [OR]
RewriteCond %{HTTP_HOST} ^www\.addon\.root\.org$
RewriteRule ^(.*)$ "https\:\/\/www\.addon\.com\/$1" [R=301,L]
here is the example with some random domains...
root domain is : bonesroot.com
addon domain is : beeraddon.com
and beerroot.com files are in the folder bones.com/beer on the server
so I want to create immediate redirection from bonesroot.com/beer to beeraddon.com
is that possible or will it affect the server?
this video explains what I want to do
https://www.youtube.com/watch?v=cRm6deeeTVY
and here is the code they recommend
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/addonfolder/(.*)$
RewriteRule ^(.*)$ - [L,R=404]
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/addonfolder/(.*)$
RewriteRule ^(.*)$ - [L,R=404]
This is the right idea, but it only triggers a 404. To redirect from https://root.example/addon.example/foo to https://addon.example/foo you would need to do it like this:
# Redirect requests to the subdirectory the addon domain points to
RewriteCond %{HTTP_HOST} ^(www.)?root\.example$ [NC]
RewriteRule ^(addon\.example)(?:$|/(.*)) https://$1/$2 [R=301,L]
This assumes that the subdirectory /addon.example is the same as the name of the addon domain, as described initially in your question. (However, for some reason, you have changed this convention later in your question?! *1)
The $1 backreference contains the subdirectory name (the same as the name of the addon domain). The $2 backreference contains the URL-path less the initial slash prefix.
The RewriteCond directive that you previously had that checked against the REQUEST_URI server variable is not required as this check is better performed in the RewriteRule directive itself.
Test first with a 302 (temporary) redirect to avoid caching issues.
*1 If the name of the subdirectory is different to the name of the addon domain then you will need to hardcode this instead. For example:
# Redirect requests to the subdirectory the addon domain points to
RewriteCond %{HTTP_HOST} ^(www.)?root\.example$ [NC]
RewriteRule ^addon-directory(?:$|/(.*)) https://addon.example/$1 [R=301,L]
TIP: Addon domains (cPanel?) don't need to point to subdomains that point to subdirectories off the main domain. They can point anywhere... including areas outside of the main domains document root. This would avoid having to implement these redirects to begin with.
OK I will explain again. I will use fake domains in this case but very similar to my actual domains
The root domain is alter.org
addon domain is numero.com
numero.com files reside inside alter.org/numero/ folder
I want to keep my current redirects which are also
numero.alter.org/foo which redirects to numero.com
what I have in htaccess is this
RewriteCond %{HTTP_HOST} ^numero\.alter\.org$ [OR]
RewriteCond %{HTTP_HOST} ^www\.numero\.alter\.org$
RewriteRule ^(.*)$ "https\:\/\/www\.numero\.com\/$1" [R=301,L]
and I want to add also redirect which redirects
alter.org/numero/foo to numero.com/foo
because I saw one google search result like that and it is duplicate content...Immediately when google bot hits the alter.org/numero/foo it needs to be redirected to numero.com/foo
Please tell me how to add a new redirect to the existing one

.htaccess redirect folder to subdomain

I've tried applying a few of the answers found on stackoverflow, but either I'm missing something or I'm plain dumb.
Basically I got a main domain name. This domain already has a non-www redirect. So http://domain.com becomes http://www.domain.com. This domain also has a mobile version found inside the the 'm' folder. So accessing the domain name like http://www.domain.com/m/ works and so does http://m.domain.com. What I'm trying to achieve is simple: anyone whom goes to the site via http://www.domain.com/m/, or http://www.domain.com/m/about should be redirected to the subdomain version so to http://m.domain.com or http://m.domain.com/about in the second case listed above.
Whatever I tried implementing ended up with errors, either I managed to disable direct access to m.domain.com, but it worked via domain.com/m/, or redirect loops.
Thanks!
You can use this code in your DOCUMENT_ROOT/.htaccess file of domain.com main .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?(domain\.com)$ [NC]
RewriteRule ^m/(.*)$ http://m.%1/$1 [L,NC,R=302]
# non-www to www
RewriteCond %{HTTP_HOST} !^(m|www)\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,NC,R=302]

htaccess apply rule to all but two domains

here's the current situation
we have multiple domains on same server (brand protection) pointing at the same content.
All have been redirected to one default domain with 301 redirection.
However, i got a task to exclude one domain from the rule (with or without www).
Rewritecond %{HTTP_HOST} !^www\.basicdomain\.com
RewriteRule ^(.*)$ http://www.basicdomain.com/$1 [R=301,L]
So after the change it should say
If user types basicDomain2.com or www.basicDomain2.com do nothing.
If user types any other doman name with or without www, make redirection to www.basicdomain.com
Can anyone help me with this?
Have you tried this one:
Rewritecond %{HTTP_HOST} !^(www\.)?basicdomain\.com
RewriteRule ^(.*)$ http://www.basicdomain.com/$1 [R=301,L]
The ()? arround www. should make it optional.

htaccess redirect multiple domains to central domain

Hi if have 3 domain names with a dns that point at one central site with trough a central htaccess file.
I would like to redirect the site by dns to the central site + subdirectory containing the domain name.
for example:
testsite1.com => thesite.com/testsite1/
testsite2.com => thesite.com/testsite2/
testsite3.com => thesite.com/testsite3/
my idea
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.bla1.com$ [AND]
RewriteCond %{HTTP_HOST} !^bla1.com$
RewriteRule ([^.]+)\.com(/[*]) http://bla2.com/$1/$2 [R=301,L]
The thing is, RewriteRule is not applied to a HTTP_HOST, only on a REQUEST_URI.
So You can do this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?example.com$
RewriteRule (.*) http://example.com/%{HTTP_HOST}/$1 [L,R,QSA]
Note however, that ".com" will not be stripped from the hostname that is passed in the new path
Btw, "301" is "Moved Permanently". I am not sure You want that, but anyway, it is a common good practice to first use 302 status when redirecting until you're done testing and everything is finalized

simple subdomain set up for local development (htaccess?)

I am trying to set up a website, domain
examplesproject.co.uk
with a subdomain which is for the moment called
sub.examplesproject.co.uk .
I am with Bluehost and so I have set up the subdomain and I have got the same document root both for examplesproject.co.uk and sub.examplesproject.co.uk but I want content for
sub.examplesproject.co.uk
to actually be located at
examplesproject.co.uk/sub .
So why didn't I set up the document root at examplesproject.co.uk/sub for the sub-domain? Because then, in local development I would need to treat the two domains as completely separate and that would mean no relative urls which seemed silly given that the subdomain folder is just tantalisingly inside the main domain.
However, if someone browsed to sub.examplesproject.co.uk they would get the same content as examplesproject.co.uk, which I don't want, so I set up htaccess rewrite in the root folder like so:
#rewite sub-domain to sub directory
RewriteCond %{HTTP_HOST} ^[www\.]*sub.examplesproject.co.uk [NC]
RewriteCond %{REQUEST_URI} !^/sub/.*
RewriteRule ^(.*) /sub/$1 [L]
That works, however if you browse to examplesproject.co.uk/sub you can still see the content and I don't want to have two locations for the same content. However if I rewrite this sub-directory to show the sub-domain in the browser address, then I create a loop where it keeps feeding round.
As another side-point, I want my main site to be forced to use www, so I also have the following in my root htaccess..
#force add www on main domain
RewriteCond %{HTTP_HOST} ^examplesproject.co.uk$
RewriteRule ^(.*)$ http://www.examplesproject.co.uk$1 [R=301,L]
However, I want to force the sub domain not to have a www infront i.e. http://sub.examplesproject.co.uk and NOT http://www.sub.examplesproject.co.uk. To do this I am trying the following but it doesn't seem to work for other directories within the sub-domain (if that makes sense). Anyhows this is the code which I put in the sub directory (ie at examplesproject.co.uk/sub):
#force remove www on sub-domain
RewriteCond %{HTTP_HOST} ^www.sub.examplesproject.co.uk [NC]
RewriteRule ^(.*)$ http://sub.examplesproject.co.uk/$1 [L,R=301]
Thought I'd mention in case it affects things.
So, my question is, how do I achieve a nice set-up where:
I can use relative URLs for developing and implementing my subdomain.
browsing to http://sub.examplesproject.co.uk shows the content of http://www.examplesproject.co.uk/sub
browsing to http://www.examplesproject.co.uk/sub doesn't duplicate the sub-domain by showing the content (for SEO purposes)
and also
Main domain examplesproject.co.uk is forced to use www - http://www.examplesproject.co.uk.
Sub domain sub.examplesproject.co.uk is force NOT to use www - http://sub.examplesproject.co.uk.
If anyone can help, I would be really grateful. By the way, locally I have set up virtual hosts http://examplesproject and http://sub.examplesproject using wamp and hosts file to replicate the online behaviour.
Thanks alot for reading. Answers/suggestions welcome.
Sorry about that Tim Post! I have put the content in this time! Nice one for looking at this. Hope that this helps someone.
This is the solution that worked for me (thanks to Jim (jdMorgan) at webmasterworld for this - http://www.webmasterworld.com/apache/4254301.htm)..
Put all of these rules, in this order, into the root .htaccess:
# Externally redirect direct client requests for test subdomain subdirectory paths to the test subdomain
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /sub/([^\ ]*)\ HTTP/
RewriteRule ^sub/(.*)$ http://sub.examplesproject\.co\.uk [R=301,L]
#
# Externally redirect all non-canonical, non-blank, non-test-subdomain hostname requests to canonical "www" main domain
RewriteCond %{HTTP_HOST} !^(www\.examplesproject\.co\.uk)?$
RewriteCond %{HTTP_HOST} !^([^.:]+\.)*sub\.([^.:]+\.)*examplesproject\.co\.uk [NC]
RewriteRule ^(.*)$ http://www.examplesproject.co.uk$1 [R=301,L]
#
# Externally redirect non-canonical subdomain hostname requests to canonical test subdomain
RewriteCond %{HTTP_HOST} ^([^.:]+\.)*sub\.([^.:]+\.)*examplesproject\.co\.uk [NC]
RewriteCond %{HTTP_HOST} !^sub\.examplesproject\.co\.uk$
RewriteRule ^(.*)$ http://sub.examplesproject.co.uk/$1 [R=301,L]
#
# Internally rewrite sub-domain requests to subdirectory path
RewriteCond %{HTTP_HOST} ^sub\.examplesproject\.co\.uk$
RewriteCond $1 !^sub/
RewriteRule ^(.*)$ /sub/$1 [L]
Checking THE_REQUEST in the now-first rule prevents the infinite redirection loop problem you encountered.
Note that exact hostnames are now enforced due to the very-careful use of case-sensitivity and anchoring.
Nice one aiit!

Resources