How to redirect 301 multi domain site - .htaccess

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]

Related

.htaccess RewriteRule change domain .com to .jp

I want to redirect requests on abc.com to abc.jp by adding this line to .htaccess file
RewriteRule ^(.*)abc\.com(.*)$ $1abc.jp$2
But it didn't work. So confusing...
Try using RewriteCond to match the domain, then redirect using RewriteRule
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?abc.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*) http://abc.jp$1 [NC,L,R=301]

htaccess redirecting one url to load from another

I am trying to redirect one of my URLs in my website, but I get error 404 not found
I am trying to redirect this url: http://www.stoikovstroi.com/bg/противопожарни-врати
to be loaded from this one: http://www.stoikovstroi.com/vina/index.php/protivpojarni-vrati
RewriteEngine On
RewriteCond %{HTTP_HOST} ^stoikovstroi\.com\bg\противопожарни-врати$ [OR]
RewriteCond %{HTTP_HOST} ^www\.stoikovstroi\.com\bg\противопожарни-врати$
RewriteCond %{REQUEST_URI} !^/vina/index.php/protivpojarni-vrati
RewriteRule (.*) /vina/$1/index.php/protivpojarni-vrati
Use this rule in site root .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?stoikovstroi\.com$ [NC]
RewriteRule ^bg/противопожарни-врати/?$ /vina/index.php/protivpojarni-vrati [L,NC,R=302,B]
Use this rule in /vina/.htaccess directory:
RewriteEngine On
RewriteBase /vina/
RewriteRule ^((?!index\.php/).*)$ index.php/$1 [L,NC]

.htaccess 301 redirect from main domain to subdomain

What should I put in .htaccess to redirect all links from main domain to subdomain? I have it like that but it doesnt wok:
RewriteCond %{HTTP_HOST} ^http://example.com/$
RewriteRule ^(.*)$ http://subdomain.example.com/$1 [R=301,L]
Have you tried without the http:// and the / at the end in the first line ?
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(.*)$ http://subdomain.example.com/$1 [R=301,L]

htaccess subdir to subdomain and subdomain to subdir redirect problem

I'm trying to accomplish the following:
http:// www.example.com/site/abc with a http 301 redirect to subdomain http:// abc.example.com
and back again within Apache:
http:// abc.example.com --> /site/abc
I want both redirects to be defined in the .htaccess in the root folder.
I've tried several combinations, but unfortunately without any luck. This is what I have now:
# 1. redirect uris which start with www. to the domain without www.
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com$
RewriteRule ^(.*)$ http://%1.example.com/$1 [R=301,L]
# 2. rewrite http://host/site/<name>/<uri> => http://<name>.host/<uri>
RewriteCond %{HTTP_HOST} ^example\.com
RewriteCond %{REQUEST_URI} ^/site/([^/]+)
RewriteRule ^(.*) http://%1.example.com/$1 [R=301,NC,L]
# 3. internal redirect to the corresponding directory
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^(.*)$ site/%1/ [L,NC]
I receive a 500 server error instead.
Thanks in advance!
Just to clarify in your original question you said you need this redirection:
http://www.example.com/site/abc => http://abc.example.com/site/abc (**site/abc also present** in destination URL)
But later in your comment you suggested:
http://www.example.com/site/abc/xyz/part?id=123&name=lmn => http://abc.example.com/xyz/part?id=123&name=lmn (**site/abc missing** from destination URL)
Assuming your comment are right, please try this in your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule site/(.+)/(.*) http://$1.example.com/$2 [R=301,L]
This will redirect www.example.com/site/foo/bar* to foo.example.com/bar* with 301 status to the browser.
Assuming /site/abc/xyz/part is a actual physical file on disk try following (if actual file got some extension then append it).
Also add QSA flag so that query string is appended.
# 1. redirect uris which start with www. to the domain without www.
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com$
RewriteRule ^(.*)$ http://%1.example.com/$1 [R=301,L,QSA]
# 2. rewrite http://host/site/<name>/<uri> => http://<name>.host/<uri>
RewriteCond %{HTTP_HOST} ^example\.com
RewriteCond %{REQUEST_URI} ^/site/([^/]+)
RewriteRule ^(.*) http://%1.example.com/$1 [R=301,NC,L,QSA]
# 3. internal redirect to the corresponding directory
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^(.*)$ site/%1/ [L,NC,QSA]

.htaccess redirect subfolder to HTTPS

I'm trying to write a RewriteRule for my .htaccess file. Specifically, whenever a user accesses a specific subdirectory, I would like it to Rewrite to force an HTTPS connection.
For example, whenever someone accesses: http://www.mydomain.com/subdirectory (and any other sub-directories of that "subdirectory").
I'd like it to rewrite to https://www.mydomain.com/subdirectory
I've tried the following, but it appears to create a loop:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mydomain.com/subdirectory/$1 [R=301,L]
Also, this .htaccess file is placed in the root of my domain.
Any ideas on how to modify my RewriteRule?
Many Thanks!
I would put this into the domain's root directory:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(subdirectory/.*)$ https://www.mydomain.com/$1 [R=301,L]
This work for me, this allow you to redirect to https a specific folder, just add an htaccess file inside of the folder with the following content:
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Ahh, its a combo of the two. MonoMano - you've omitted the subdirectory in the first part of the RewriteRule, therefore directing ALL traffic to the HTTPS subdomain address. I found that checking for 'off' was more reliable than checking for !=on, dont ask me why!
You'd want to add the subdirectory back in as per Floern's response, see below:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(subdirectory/.*)$ https://www.mydomain.com/subdirectory/$1 [R=301,L]
Add the lines given below to .htaccess file of that subdirectory:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.com/subdirectory/$1 [L,R=301]
Use this if you don't want the address bar to show the subfolder. It will redirect yourdomain.com to yourdomain.com/subfolder but will look like you are still on yourdomain.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?yourprimarydomain.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/$1
RewriteCond %{HTTP_HOST} ^(www.)?yourmaindomain.com$
RewriteRule ^(/)?$ subfolder/index.php [L]
Try This
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
This will remove www prefix , and force https:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://example.com%{REQUEST_URI} [NE,R=301,L]
RewriteRule ^((?!subdirectory/).*)$ /subdirectory/$1 [L]
Change example.com for you main domain and subdirectory for Sub Directory
Try
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.mydomain.com/subdirectory/$1 [R=301,L]

Resources