.htaccess: Redirect pages if not accessed within subdomain - apache

Scenario:
Main domain domain.com points to */home/domain/www*
Addon domain addon.com points to */home/domain/www/addon/www*
Question: How do I do the following:
http://addon.domain.com instead of http://domain.com/addon
http://addon.domain.com/page.html instead of http://domain.com/addon/page.html

try adding these rules to the htaccess file in the /home/domain/www directory:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^addon/?(.*)$ http://addon.domain.com/$1 [L,R=301]

Related

Redirect all the requests coming from specific domain

I want to redirect all the requests coming from specific domain to specific directory. To be more precise I have two different domains i.e example.com, example.tk and both the domains point to a same server. The sever has two different directories i.e cars, bikes. I have created .htaccess file on root but not able to do the configurations. I want that all request coming from example.com should be redirected to cars directory. And all requests coming from example.tk should be redirected to bikes directory.
How can I achieve that successfully?
This .htaccess file will redirect http://example.com/file.html to http://example.com/cars/file.html and http://example.tk/file.html to http://example.tk/bikes/file.html:
Filename: .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} example.com$ [NC]
RewriteCond %{HTTP_HOST} !cars
RewriteRule ^(.*)$ http://example.com/cars/$1 [R=301,L]
RewriteCond %{HTTP_HOST} example.tk$ [NC]
RewriteCond %{HTTP_HOST} !bikes
RewriteRule ^(.*)$ http://example.tk/bikes/$1 [R=301,L]
i think it will help you
for more reference : here

Redirect root of website only with apache and .htaccess

I am looking to redirect only the root of my domain to another domain with apache + .htaccess - However I would like to maintain current directories.
Example:
http://www.domain-one.com/ would redirect to http://www.domain-two.com/
However anything after the / would be ignored, examples:
http://www.domain-one.com/contact
http://www.domain-one.com/?param=1
You can use this rule in the site root .htaccess of domain-one:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain-one\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/?$ http://www.domain-two.com/ [L,R=301]

htaccess redirect root only without changing url

i have 2 sites:
www.test1.com
and
www.test2.com
With the below code i can redirect from test1.com to test2.com without changing the url
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test1.com
RewriteRule ^(.*) http://test2.com/$1 [P]
But if i try to access a folder in test1 for example test1.com/folder it also redirects.
So how i can only redirect the root folder but still without changing the url but be able to access a subfolder in my main domain?
Just change your regex to allow only landing page:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test1\.com$ [NC]
RewriteRule ^/?$ http://test2.com [P]

how to redirect domain to dynamic subdomains?

I have store in mysite.com and in this site, there is some store like apple and... .
users can view this stores with virtual subdomains like http://apple.mysite.com
Therefore, I create .htaccess in root file manager:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.mysite.com$
RewriteCond %{HTTP_HOST} ^(.*)\.mysite.com$
RewriteRule ^$ /stores.php?id_store=%1 [P,L]
then in "Subdomains" in Cpanel I create *.mysite.com and in "Document Root" set /public_html
Now, how can I park or redirect domain to this dynamic subdomain with htaccess code?
For example:
park or redirect applestore123.com to apple.mysite.com
I have Shared Host Server.
For applestore123.com to apple.mywebsite you have to change your htaccess rule like:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^applestore123.com$ [NC]
RewriteRule (.*) apple.mysite.com$ [QSA,R=301,L]

Domain .htaccess redirect issue

I'm trying to create some redirects with .htaccess but I never manage to get it fully functional. Maybe someone here can help me.
What I need is:
http://domain.se and http://domain.com to redirect to http://www.domain.com.
I also need http://domain.se/somefolder, http://domain.com/somefolder as well as http://www.domain.se/somefolder to redirect to http://www.domain.com/folder.
I've tried to accomplish this myself but all I end up with is errors about data not being sent.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# folder rewrite
RewriteRule ^somefolder$ folder [L]
# domain redirect
RewriteCond %{HTTP_HOST} =domain.com [OR]
RewriteCond %{HTTP_HOST} =domain.se
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
This is to be placed in .htaccess file in website root folder. If placed elsewhere some tweaking may be required.
First rule will rewrite (internal redirect) requests to /somefolder to /folder. If you need this to be 301 Permanent Redirect, then replace [L] by [R=301,L]
Second rule will do domain redirect job. This rule will ONLY redirect if domain is domain.com or domain.se. If you want to have redirect from ANY domain name (that your webserver is configured can serve) to www.domain.com then replace those 2 RewriteCond lines with this one: RewriteCond %{HTTP_HOST} !=www.domain.com.
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301]
That should meet all of your requirements. All requests that are not www.domain.com will be redirected to that domain, with the request URI intact.