Redirect domains to path on main domain - php

I have some domains:
http://domainmain.com
http://domainone.com
http://domaintwo.com
My secondary domains are currently hosted under the main domain. No sub-directories, no other paths. So every domain get the contents of http://domainmain.com.
For better understanding: These files points all to the same file: http://domainmain.com/index.php, http://domainone.com/index.php, http://domaintwo.com/index.php.
For every domain I have a folder located at http://domainmain.com:
domainname folder / path
-------------- -----------
domainmain.com /
domainone.com /domainone
domaintwo.com /domaintwo
My goal is to redirect every domain to the corresponding dir / path http://domainone.com.
For example: http://domainone.com has to show the content of path /domainone. The visiter has to see http://domainone.com. This also should work: http://domaintwo.com/images shows http://domainmain.com/images.
Some code I started with in the .htaccess file:
RewriteCond %{HTTP_HOST} domainone.com [NC]
RewriteCond %{REQUEST_URI} !^/domainone
RewriteRule ^(.*)$ /domainone/$1 [NC,L]
And some PHP (but I want to use redirect instead of file_get_contents():
if ($_SERVER['SERVER_NAME'] == 'domaintwo.com') {
echo file_get_contents('http://domainmain.com/domaintwo');
die();
}
Note: It is only possible to have an .htaccess file at http://domainmain.com. My server runs PHP5.

Your question is quite similar to this one asked on the web-master's section;
How to redirect different domains to separate subdirectories.

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(domainone)\.com$ [NC]
RewriteRule (?!^domainone(/.*|)$)^.*$ /%1%{REQUEST_URI} [NC,L]
RewriteCond %{HTTP_HOST} ^(domaintwo)\.com$ [NC]
RewriteRule (?!^domaintwo(/.*|)$)^.*$ /%1%{REQUEST_URI} [NC,L]

Related

.hatccess file to redirec to subfolder/subfolder

I am nto able to rewrite URL as I need.
I have the following document structure:
1) root - root folder where .htaccess should be placed in
2) root/folder1 - subfolder
3) root/folder1/public - this is folder where index.php is
Thus I need that mydomain.com would open index.php inside "public" folder. And all other requests would go via it.
I tried this, but it doesn't work (p.s. I am writing rewrite rules first time). I put it inside root folder.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$
RewriteCond %{REQUEST_URI} !^/$1
RewriteRule !^folder1/public /folder1/public%{REQUEST_URI} [L,NC]
I have a similar .htaccess like yours, so I edited a bit mine. Does this worked for you?
The condiftion for .css|.js is needed otherwise it will send as plain/text instead of normal css/js
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(css|js)$
RewriteRule ^([^?]*)$ root/folder1/public/index.php [NC,L,QSA]

.htaccess to redirect parts of a URL to local files

I have a custom web app and the file structure works like this:
/apps/calendar/frontend/index.php
/apps/calendar/frontend/view/index.php
/apps/calendar/backend/index.php
/apps/calendar/backend/edit/index.php
/apps/calendar/backend/add/index.php
/apps/calendar/backend/view/index.php
I'm trying to write a .htaccess file to help redirect the files so they cant see the 'real' path.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/admin
RewriteRule ^([^/\.]+)/(.*)/(.*)($|/$) /apps/$1/frontend/$2/$3 [NC,L]
RewriteRule ^([^/\.]+)/(.*)($|/$) /apps/$1/frontend/$2 [NC,L]
RewriteRule ^([^/\.]+)($|/$) /apps/$1/frontend/ [NC,L]
When I visit localhost/calendar it should map redirect to /apps/calendar/frontend/index.php. But when I visit localhost/calendar/add it gives me a 301 (permanent move) then shows the full page of localhost/apps/calendar/frontend/add/index.php in the console. Anyone got any ideas why this would happen? Or a better way around this? The apps might have heaps of sub-directories so, I'm not particularly keen on having a rule for ever subdirectory combination.
As you can see also I have a /admin path, which would load the /backend/ parts of the app. I would assuming I can do the similar code with the prefix of /admin?
This question might also be of your interest: Create blog post links similar to a folder structure.
Given that your .htaccess is located on the root folder of your domain /home/youraccount/public_html/.htaccess, it would look like this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/(admin|apps) [NC]
RewriteCond %{DOCUMENT_ROOT}/apps/$1 -d
RewriteRule ^([^/]+)/?(|.*)$ /apps/$1/frontend/$2 [NC,L]
RewriteCond %{REQUEST_URI} !^/apps [NC]
RewriteCond %{DOCUMENT_ROOT}/apps/$1 -d
RewriteRule ^admin/([^/]+)/?(|.*)$ /apps/$1/backend/$2 [NC,L]
Let's say the user access:
http://domain.com/calendar
http://domain.com/calendar/
http://domain.com/calendar/add
All the above would redirect to
/apps/calendar/frontend/index.php
/apps/calendar/frontend/index.php/
/apps/calendar/frontend/index.php/add
And the if the user access:
http://domain.com/calendar/admin
http://domain.com/calendar/admin/
http://domain.com/calendar/admin/add
It would go to:
/apps/calendar/backend/index.php
/apps/calendar/backend/index.php/
/apps/calendar/backend/index.php/add
So it would make index.php your controller for each end:
/apps/calendar/frontend/index.php
/apps/calendar/backend/index.php

View folder instead of public_html

How can i "redirect" my page, to show /v4-beta/ instead of ./ when accesing my domain?
Not only redirect but show the content of v4-beta on the domain and not www.domain.com/v4-beta ? but showing www.domain.com but all content is from v4-beta ?
A simple solution would be to include the subfolder's index file in the top-level index file.
public_html/index.php:
<?php
include("v4-beta/index.php");
?>
You are looking for the rewriting module that comes with the apache http server:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/v4-beta/
RewriteRule ^(.*)$ /v4-beta/$1 [L]
The module has to be loaded first. Then you can use those commands inside the mail server configuration or inside so called .htaccess files. The frist option is the preferred one.
Htaccess :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteCond %{REQUEST_URI} !^/v4-beta/
RewriteRule (.*) /v4-beta/$1

301 redirect to same name file, same server, but differnet domain name

I recently did an overhaul of a website in which we switched to a new master domain name for that website. The old domain is still active, and needed to be redirected to the new domain name.
The site still has the same root folder, and so there are a couple of files that were updated, but have the same name (i.e. index.php, about.php, contact.php).
I manually redirected all the files (via .htaccess) that no longer exist to new files with a manual redirect:
redirect 301 /old_file.php http://www.new_domain.org/new_file.php
However this will not work with the files that have the same name such as "index.php". I can still call the site by the old domain name. How do I properly refer these files so that I don't have a duplicate penalty from google?
I tried this and it did not work:
#Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.old_domain.org$[OR]
RewriteCond %{HTTP_HOST} ^old_domain.org$
RewriteRule ^(.*)$ http://www.new_domain.org/$1 [R=301,L]
Thank You
Clear your browser cache and restart it
Make sure mod_rewrite and .htaccess are enabled through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Code
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?old_domain\.org$ [NC]
RewriteRule ^ http://www.new_domain.org%{REQUEST_URI} [R=301,L]
Take a look at using the canonical tag as an alternative.
http://www.seomoz.org/blog/cross-domain-canonical-the-new-301-whiteboard-friday

Directory structure change using .htaccess

I want to move all my web site files (even including index.php) into a subdirectory (for exp: "abc")
For example
BEFORE:
public_html
index.php
a.file
directory
an.other.file
...
AFTER:
public_html
abc_directory
index.php
a.file
directory
an.other.file
...
I want everything to work, as it was before, but i don't want to make any redirections (visible).
People should enter "http://myexmaplesite.com/directory/an.other.file/" and by .htaccess apache serve them "http://myexmaplesite.com/abc_directory/directory/an.other.file/" BUT WITHOUT EXTERNAL REDIRECTS (301,302 etc.)
How could I route all requests to a subdirectory using mod_rewrite?
Try this mod_rewrite rule in your document root:
RewriteEngine on
RewriteRule !^directory/ directory%{REQUEST_URI} [L]
Or in general:
RewriteEngine on
RewriteCond $1 !^directory/
RewriteRule ^/?(.*) directory/$1 [L]
This should be even applicable in server or virtual host configurations.
Something like
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ directory/$1 [L,QSA]

Categories