htaccess redirect root only without changing url - apache

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]

Related

.htaccess: 301 redirect not working in .htaccess in rewritten root path

I have a domain called example.com which points to /public_html/ on my server.
In /public_html/.htaccess I have the following code to change the root path for my domain example.com to /public_html/example_path/:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteRule !example_path/ /example_path%{REQUEST_URI} [L]
This is working very well.
Now, I want to redirect example.com/test.png to example.com/images/test.png. To do this, I'm using the following code in /public_html/example_path/.htaccess:
RewriteEngine On
Redirect 301 /test.png /images/test.png
Unfortunately, this isn't working, there's no redirection. The redirect is only working if I put the code in /public_html/.htaccess but not in /public_html/example_path/.htaccess.
Why that?
Inside your /public_html/.htaccess have redirect rule before example_path rule:
RewriteEngine On
RewriteRule ^test\.png$ /images/test.png [L,NC,R=301]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule !example_path/ /example_path%{REQUEST_URI} [L,NC]
Note that you cannot have image redirection rule inside /example_path/.htaccess because your image URL doesn't have /example_path/.

Rewriting sub-domain leads to a HTTP redirect rather

What I'm trying to do here is rewrite my https://demo.example.com/ sub-domain to my https://example.com/ domain (in another folder), without changing the URL. However, the sub-domain keeps on redirecting me to the folder. I'm not great at HTACCESS and rewriting, but this should be an easy task to do.
This is my current HTACCESS file:
RewriteEngine On
RewriteRule ^license/?(.*)$ /sites/site-licenses/$1.php [L]
RewriteRule ^portfolio/?(.*)$ /sites/site-portfolios/$1.php [L]
RewriteCond %{HTTP_HOST} ^demo\.hamiltondev\.net
RewriteRule ^(.*)$ https://hamiltondev.net/sites/site-demos/$1 [L,NC,QSA]
The first two rewrite rules is for the main domain https://example.com/license/--- and https://example.com/portfolio/---.
When a full url is used as a target in a RewriteRule, it automatically becomes a redirect. What you need is either use an absolute path (if demo and main domain share the same document root folder) or use a proxy request.
both domains share document root folder
RewriteCond %{HTTP_HOST} ^demo\.hamiltondev\.net$ [NC]
RewriteRule ^(.*)$ /sites/site-demos/$1 [L,NC,QSA]
otherwise, proxy
RewriteCond %{HTTP_HOST} ^demo\.hamiltondev\.net$ [NC]
RewriteRule ^(.*)$ https://hamiltondev.net/sites/site-demos/$1 [P,NC,QSA]

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: How can I keep the redirection and remove the subdirectory path from the urls please?

I am trying to direct traffic to a subdirectory using .htaccess
The subdirectory is: /Release2/code.
The following is successfully redirecting but its is not hiding /Release2/code from the urls
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$
RewriteCond %{REQUEST_URI} !^/Release2/code
RewriteRule ^(.*)$ /Release2/code/$1 [L]

.htaccess: Redirect pages if not accessed within subdomain

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]