htaccess 2 domains with same parameter - .htaccess

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.

Related

Redirect multilevel subdomain to a folder .htaccess

I have a subdomain meet.example.com that points to the app folder and I want api.meet.example.com to point to the api folder.
The folder structure is like this
public_html - root folder
app
api
Then this is the rule that I have in my .htaccess I have in the root folder. The first rule works fine and opens the content from the app folder, but the second rule (api.meet.example.com) is not opening the content from the api folder.
RewriteCond %{HTTP_HOST} ^meet\.example\.com$
RewriteCond %{REQUEST_URI} !^/app/
RewriteRule (.*) /app/$1 [L]
RewriteCond %{HTTP_HOST} ^api\.meet.example\.com$
RewriteCond %{REQUEST_URI} !^/api/
RewriteRule (.*) /api/$1 [L]
What I am doing wrong here?
SOLUTION
Thanks for #anubhava question about the VirtualHost config I just realized that I did redirect both domains from one hosting to another, but didn't add both domains to the new hosting just the meet.example.com, assuming that as api.meet.example.com is actually a subdomain of the already added then I will not need to add it.
Adding api.meet.example.com as a domain to the new hosting fixed the issue.

mod_rewrite, subdomains and htaccess in CodeIgniter

Right now i'm working on a webshop project in codeigniter.
In the end there will be multiple webshops on subdomains, and a frontpage on the main domain.
It's supposed to be all dynamic so that the webshops get their own subdomains.
The shop information is in the database. Lets say I have a mysql table which contains info like this:
webshop: Bobs shop
webshop_subdomain: bobsshop
And so on.
Now in a .htaccess file I want to make a rewrite rule so that bobsshop.mydomain.com will be rewritten to mydomain.com/bobsshop/ since i'm working in Codeigniter with controllers.
You should bear in mind that the index.php also has to be rewritten out and there should also be the possibility to make subpages like bobsshop.mydomain.com/contact/
I will make a php script which will check if the subdomain exists in the database and if it doesn't exist then the visitor will see a 404.
My DNS setup looks like this ATM:
mydomain.com xxx.xxx.xxx.xxx
*.mydomain.com xxx.xxx.xxx.xxx
Where xxx.xxx.xxx.xxx is my server IP.
I've really never worked with htaccess / mod_rewrite before.
Can it be done? And are there anyone who can help me make this?
Sorry for my English. I hope you understand what I'm trying to do.
Thanks in advance
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^((?!www\.)(?!mydomain\.com)[^\.]+)\.
RewriteRule ^(.*)$ http://www.mydomain.com/index.php?/%1/$1 [L]
Input URL - http://adasd.mydomain.com/test
1 RewriteCond %{HTTP_HOST} ^((?!www\.)(?!mydomain\.com)[^\.]+)\.
This condition was met
2 RewriteRule ^(.*)$ http://www.mydomain.com/index.php?/%1/$1 [L]
This rule was met, the new url is http://www.mydomain.com/index.php?/adasd/test
The tests are stopped, using a different host will cause a redirect
Output URL - http://www.mydomain.com/index.php?/adasd/test
You can then remove the index.php from the URL by
Open config.php from system/application/config directory and replace
$config['index_page'] = "index.php" with $config['index_page'] = ""
Implementing the above .htaccess file
In some case the default setting for uri_protocol does not work properly. To solve this problem just replace $config['uri_protocol'] = "AUTO" with $config['uri_protocol'] = "REQUEST_URI" in system/application/config/config.php

htaccess command to prevent master site access via subdirectory?

I have hosting setup with a master domain (mapped to the web root) and then a number of addon domains (each with their own folder within the web root). At the moment you can visit www.masterdomain.com/addondomainsubdir and reach the same page as you would if you visited www.addondomain.com (which maps to /public_html/addondomainsubdir). I want to prevent this so if you visit www.masterdomain.com/addondomainsubdir then it will do a 301 redirect to www.addondomain.com. The new addondomain.com site is a single page site so it does not have to map any additional pages.
Adding rules to the htaccess file in the web root does notaffect anything as the subdir exists which is wierd as i thought the htaccess command should work even if there is a matching subdir (i've tried the following which works when there's no matching subdir):
RewriteRule ^addondomainsubdir?$ http://www.addondomain.com [NC,R=301,L]
Logically given it's reaching this directory I figure i need to add a command within the htaccess file in the addondomainsubdir directory however nothing appears to have any effect (i've got various other rules setup and they work fine).
I would be massively grateful if anyone explain the best way to rectify this?
Thanks so much for your help,
Dave
I know this is an old post, but it has never been successfully answered. So for all of you finding this via search, this should do what the OP is asking.
Add this line to your .htaccess file:
redirect permanent /addondomainsubdir/ http://www.addondomain.com
Try these rules in your .htaccess:
Options +FollowSymlinks -MultiViews
RewriteEngine on
# for http
RewriteCond %{HTTP_HOST} ^(www\.)?masterdomain\.com$ [NC]
RewriteCond %{SERVER_PORT} =80
RewriteRule ^([^/]+)/?$ http://www.$1.com/ [R=301,L]
# for https
RewriteCond %{HTTP_HOST} ^(www\.)?masterdomain\.com$ [NC]
RewriteCond %{SERVER_PORT} =443
RewriteRule ^([^/]+)/?$ https://www.$1.com/ [R=301,L]
Instead of putting a rule in your main .htaccess, I would make make a .htaccess for each add-on domain, putting each one in the respective subdirectory.
RewriteEngine on
RewriteCond %{HTTP_HOST} masterdomain\.com$ [NC]
RewriteRule ^addondomainsubdir(.*)$ http://www.addondomain.com/$1 [R=301,L]

.htaccess - subdomain not working with www

I see a lot of questions about it here on SO, so I'm posting here:
I have a domain www.example.com and I have set .htaccess file to redirect all example.com to www.example.com
Now, I made (through my plesk 10 interface) a subdomain abc.example.com and what I would like is to also have www.abc.example.com so I entered (also in plesk) this:
www.abc.example.com. CNAME abc.example.com.
But it's not working. Do I need to reload/restart dns?(if so, please tell me how?) Or do I just need to wait certain time for this until it propagates?
Since my mentioned CNAME didn't work, I also added the .htaccess (which might be wrong (i know, not much of a server person :( )) in the abc folder which looks like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^abc.example.com$
RewriteRule ^(.*)$ http://abc.example.com/$1 [R=301]
but with no luck, so please shed some light.
The solution which worked for me in the end:
In Plesk I made one subdomain.domain.com pointing to lets say abc folder, and then I added the www.subdomain.domain.com also through Plesk but pointed it to the same abc folder. Then I added the .htaccess file inside this abc folder (where all other files for this subdomain reside) which now guarantees that all requests to subdomain.domain.com get redirected to www.subdomain.domain.com. Here is my .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.subdomain.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.subdomain.domain.com/$1 [R=301,L]
Hope this will help someone beginning with this stuff like me.
Just to note, the credit for the idea of pointingthe www.subdomain inside the same folder goes to user Bryan White on serverfault
.htaccess should be like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^abc\.example\.com$ [NC]
RewriteCond %{SERVER_PORT} =80
RewriteRule ^(.*)$ http://abc.example.com/$1 [R=301,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} !^abc\.example\.com$ [NC]
RewriteCond %{SERVER_PORT} =443
RewriteRule ^(.*)$ https://abc.example.com/$1 [R=301,L]
However DNS entry for abc.example.com and www.abc.example.com might not have been propagated. You need to give it sometime (few hours may be) before you test this.
You can use nslookup on Windows or *nix to check when is your domain is available to the world.

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