.htaccess redirect subdirectory to new domain - .htaccess

I need to redirect all pages under a specific folder to another domain with different sub folders
For example:
FROM: https://www.my.domain.com/user/myuser/test.png
TO: https://www.newdomain.com/all/allegato1.png
I have try many htaccess file but all not work
RewriteEngine On
RewriteCond %{HTTP_HOST} ^my.domain.com$ [OR]
RewriteCond %{HTTP_HOST} www.my.domain.com$
RewriteRule ^user/myuser/(.*) http://www.newdomain.com/all/$1 [R=301,L]
I insert my .htaccess into the dir my.domain.com/user/myuser/
How can i made this rule? Thanks for your help

Inside /user/myuser/.htaccess following rule should work:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?my\.domain\.com$ [NC]
RewriteRule .* http://www.newdomain.com/all/$0 [R=301,L]

Related

.htacees Redirect All Subdomains Except www

I'm trying to edit my .htaccess code to work for all the below conditions. Here is the .htaccess file that I have so far:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old_domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)\.old_domain\.com$
RewriteRule ^(.*)$ https://new_domain.com%{REQUEST_URI} [R=301,L]
Here is the functionality I'm looking for:
old_domain.com -> https://new_domain.com/ #This Works
old_domain.com/any_directory/ -> https://new_domain.com/any_directory/ #This Works
www.old_domain.com -> https://new_domain.com/ #This Works
any_subdomain_besides_www.old_domain.com -> https://any_subdomain_besides_www.new_domain.com/ #This does NOT work
How should I edit my .htaccess file so that last line works, along with all the other conditions? To be clear, if the subdomain on the old domain is www, then remove it on the new domain. However if it is any other subdomain besides www, keep it in and only change the domain. Currently it removes all subdomains. Thank you!
You can use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?old_domain\.com [NC]
RewriteRule (.*) https://new_domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.+)\.old_domain\.com [NC]
RewriteRule (.*) https://%1.new_domain.com/$1 [R=301,L]
Only old_domain.com with www subdomain will be redirect
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?old_domain\.com [NC]
RewriteRule (.*) https://new_domain.com/$1 [R=301,L]

How to redirect 301 multi domain site

I like to redirect the following URL: mydomain.com/myurl to mydomain.com/newurl.
Usually, I could write:
Redirect 301 /myurl /newurl
But I need it specifically for mydomain.com because I got other domains too which I do not want to redirect.
What is a good solution?
Write a rule in .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com/\oldurl$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com\/oldurl$
RewriteRule ^(.*)$ "http\:\/\/domain\.com/\newurl$1" [R=301,L]
Try this Apache .htaccess rules:
RewriteEngine on
RewriteCond "%{HTTP_HOST}" "^mydomain\.com$"
RewriteRule "^" "http://%1/newurl" [R=301, L]
If .htaccess file is placed on root folder and newurl is relative to root folder use this code instead:
RewriteEngine on
RewriteCond "%{HTTP_HOST}" "^mydomain\.com$"
RewriteRule "^" "newurl" [R=301, L]
If your hostname is www.mydomain.com change "^mydomain\.com$" to "^www\.mydomain\.com$".
After reading here: https://www.leaseweb.com/labs/2015/10/rewritecond-and-rewriterule-tricks-for-htaccess/
I got it working:
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$
RewriteCond %{REQUEST_URI} ^/oldurl$
RewriteRule ^(.*)$ "https\:\/\/mydomain\.com\/newurl" [R=301,L]

Redirect from one directory to a subdomain using .htaccess

I want to redirect all the urls of a directory (example.com/blog/urlnames) on one domain to a sub-domain (blog.example.com/urlnames)
I tried:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^/blog$ [NC]
RewriteRule (.*) https://blog.example.com/$1 [R=301,L]
I keep getting a 404 error when I type the url on example.com:
https://example.com/blog/5-names-for-actors/
I want this to go to:
https://blog.example.com/5-names-for-actors/
You can use this rule in blog/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.*) https://blog.example.com/$1 [R=301,L,NE]
File blog/.htaccess may not be existing already so you may have to create it.

Add https enforcement to a second domain in .htaccess

My current .htaccess file looks like this:
# Use PHP56
AddHandler application/x-httpd-php56 .php
RewriteEngine On
RewriteCond %{HTTP_HOST} ^directoryhub\.net [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.directoryhub.net [R,L]
This redirects directoryhub.net, which is my root domain in public_html, to https.
However, now I want to do the same thing with a second domain (all the files of which are in a subfolder of public_html). How can I achieve this?
Don't use hardcoded host name in condition and target to make it generic for all the domains that are controlled by your .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
I realized that I was editing the .htaccess file in the root folder, which is the wrong file. I needed to edit the .htaccess file within public_html as well as the .htaccess file within each of my subdomains to the following:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://mydomain/$1 [R=301,L]
I suppose I could also delete all the .htaccess files of my subdomains and change the root .htaccess to #anubhava's answer, but it works by editing each individual file as well.

redirect subdomain but keep URL intact

I am pointing blog.website.com at website.com/blog correctly however I want the URL to remain as blog.website.com
Here is my .htaccess file
RewriteEngine on
RewriteCond %{HTTP_HOST} ^blog\.website\.com$
RewriteRule ^/?$ "http\:\/\/website\.com\/blog" [L]
How can I do this?
UPDATE
I have managed to do it using the following code in my .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^blog\.website\.com$
RewriteCond %{REQUEST_URI} !^/blog/$
RewriteRule (.*) http://website.com/blog/ [P]
However, now my blog images are not showing. If I put them into the blog folder created for the subdomain, they work, but I want to keep them inside the website.com/blog folder, how can I achieve this?
Try this rule instead:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^blog\.website\.com$
RewriteRule !^blog/ blog%{REQUEST_URI} [L]

Resources