htaccess Redirect depending on domain.com/***/ - .htaccess

I got the following Server structure:
3 Domains are pointing at the exact same Server root.
Each of them has different subfolders
In each subfolder there are multiple subfolders for each year
SERVER
DOMAIN_1
-- SITE_1
--- index.html
-- SITE_2
--- index.html
-- SITE_3
--- index.html
DOMAIN_2
-- SITE_1
--- index.html
-- SITE_2
--- index.html
-- SITE_3
--- index.html
DOMAIN_3
-- SITE_1
--- index.html
-- SITE_2
--- index.html
-- SITE_3
--- index.html
How can i redirect each domain to its depending subfolders-subfolder.
EXAMPLE:
http://domain1.de/SITE_2/
should lead to /DOMAIN_1/SITE_2/index.html
BUT only show: http://domain1.de/SITE_2/index.html
http://domain3.de/SITE_1/
should lead to /DOMAIN_3/SITE_1/index.html
BUT only show: http://domain3.de/SITE_1/index.html
At the moment i only know how to redirect depending on the added subfolders name.
But this only works for ONE Domain. Each Domain has the exact same subfolders name, so this would not work. Somehow it has to be depending on the DOMAIN_NAME.
RewriteRule ^SITE_2/(.*)$ /DOMAIN_1/SITE_2 [R=301,L]
this does NOT work:
RewriteRule ^DOMAIN_2/SITE_2/(.*)$ /DOMAIN_1/SITE_2 [R=301,L]

You can use a RewriteCond for this. In your example limit the RewriteRule to a specific domain and then redirect all requests to the correct directory:
RewriteCond %{HTTP_HOST} domain1\.com$ [NC]
RewriteRule ^SITE_2/(.*)$ /DOMAIN_1/SITE_2/$1 [END]
RewriteCond %{HTTP_HOST} domain2\.com$ [NC]
RewriteRule ^SITE_1/(.*)$ /DOMAIN_2/SITE_1/$1 [END]
...
Repeat this for all domains and folders.
If there are no other files/folders on the domain that should not be redirected, you could even simplify the RewriteRule. (e.g. RewriteRule ^(.*)$ /DOMAIN_1/$1 [END])
Cheers!

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

Adding multiple rewrite cases to .htaccess file

My webroot has two directories. One is called 'functions', and the other is called 'site'. I have added an '.htaccess' to the webroot along with the other two directories. There can be two types of incoming requests, request to view a page or request to use an api.
Now, I want a check on the .htaccess that checks if the request_uri contains '/api/' in it. If it contains, the request needs to be forwarded to the 'functions' directory. For every other request (i.e. any request_uri that does not contain '/api') the request needs to be forwarded to the 'site' directory.
I await help writing this.
Very simple. Add the following to your /.htaccess :
RewriteEngine on
#if Request_uri string contains "/api/" forward the request to /function dir
RewriteCond %{REQUEST_URI} /api/ [NC]
RewriteRule ^.+$ /function%{REQUEST_URI} [L]
# else redirect the request to /site directory
#redirect all other requests except /site and /api to /site directory
RewriteCond %{REQUEST_URI} !/(site|api) [NC]
RewriteRule ^.*$ /site%{REQUEST_URI} [L]
In the second Rule we excluded /site directory from the rule to avoid rewriting /site/ requests to /site .
Note : The second rule also rewrites your homepage / to /site .If you don't want to redirect your home directory then simply change the Rule's pattern to ^.+$ .

htaccess 2 domains with same parameter

Hope you guys can help me out with a probably simple question.
I have 2 domains on 1 sharehost server:
example1.com
example2.com
On the host i have 2 folders in the public_html folder.
public_html
-- example1
-- example2
-- htaccess
I managed to get both domains to redirect to each folder.
The problem im having is this.
Both pages have a page called "nextpage".
The url looks like this then:
http://www.example1.com/nextpage
http://www.example2.com/nextpage
but whenever I go to the second url it redirects to the first url.
Hope you can still follow me.
Below is how the htaccess is right now.
I know where the problem is but don't know how to fix it.
# pointing for the domain example1.com to folder example1
ReWriteCond %{HTTP_HOST} example1.com
ReWriteCond %{REQUEST_URI} !example1/
ReWriteRule ^(.*)$ example1/$1 [L]
# pointing for the domain example2.com to folder example1
ReWriteCond %{HTTP_HOST} example2.com
ReWriteCond %{REQUEST_URI} !example2/
ReWriteRule ^(.*)$ example2/$1 [L]
hope it makes sense.
If you require more info let me know.

.htaccess domain redirection to folder but preventing direct access with 404

I have a shared hosting in which I want to host several websites. To this end, and to keep things clear, I have redirected my primary domain (i.e. www.mydomain.com) to a folder (i.e. /mydomain.com/). However, I want to prevent direct access to this folder (www.mydomain.com/mydomain.com/) from the URL with a 404 error (not found).
This is the .htaccess in the root directory:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mydomain.com/0.1/$ [NC]
RewriteRule ^(.*)$ /mydomain.com/0.1/$1
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$ [NC]
RewriteRule ^(/)?$ mydomain.com/0.1/index.php [L]
RewriteCond %{THE_REQUEST}% ^[A-Z]{3,9}\ /mydomain\.com/ [NC]
RewriteRule . - [R=404,L]
And this is the .htaccess in /mydomain/ with a bunch of rules for nice and tidy URLs.
RewriteEngine On
RewriteRule ^about/(.+) index.php?a=about&b=$1
RewriteRule ^services/(.+) index.php?a=services&b=$1
Right now this is the result:
Accessing www.mydomain.com shows the content in /mydomain/ as hoped. However, www.mydomain.com/mydomain/ is also displayed, ignoring the 404 rule, and creating another folder -whatever the name- without an htaccess DOES throw the 404.
I've been dealing with this problem for 5 days now and tried everything. At this point I don't know if the error comes from the root .htaccess or the folder's.
Thanks in advance.
PS: Just to be clear, I have no control over the httpd.conf file.
Notes:
I forgot saying that there is an additional folder inside /mydomain.com/ called /0.1/ for version control.
If a include "RewriteOptions Inherit" after "RewriteEngine On" in the /mydomain.com/0.1/ htaccess, I get a 500 internal server error.
Deleting the /mydomain.com/0.1/ htaccess file altogether will produce the desired 404 error for www.mydomain.com/mydomain.com/0.1/ and produce a 505 in www.mydomain.com
The R flag in rewrite rule should be something between 301 to 399 , so it can not be 404!!!
your rule should be like below:
RewriteCond %{THE_REQUEST} ^/mydomain\.com/? [NC]
RewriteRule (.*) throw_404.php [L]
create throw_404.php and to send 404 status code! and add L flag to your first rule for avoiding conflicting between that rule and mine!

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