Redirect URL to another domain with hiding - .htaccess

When i write www.mysite.net/abc it should go to the adress www.example.com
But in the browser www.example.com should not be appeared, it should stay as www.mysite.net/abc
With htaccess file, how can i do that?
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\mywebsite\.net$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [P,R=301,NE]
I tried that, did not work for me

You are using the flags P (Proxy) and R (Redirection) at the same time. That does not make sense. You do not want to redirect the request.
This probably is what you are looking for:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\mysite\.net$ [NC]
RewriteRule ^/?abc(?:/(.*))$ https://www.example.com/$1 [P]
An alternate implementation, which does not require the rewriting module would be:
ProxyRequests off
ProxyPass /abc https://www.example.com/
ProxyPassReverse /abc https://www.example.com/
You certainly can implement such rules in a distributed configuration file (".htaccess"), but I would strongly recommend to use the central configuration of the http host instead.

Related

Can't map https://www.example.com to https://example.com

Trying to set up .htaccess to send requests for http://www.example.com to https://example.com. Using an .htaccess procedure like this
RewriteEngine On
RewriteCond %{REQUEST_SCHEME}#%{HTTP_HOST} ^http#(?:www\.)?(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R]
does the right thing when dealing with http://www.example.com (going to https://example.com) and either https://example.com or http://example.com. However, when I try using https://www.example.com, Chrome throws up "ERR_SSL_PROTOCOL_ERROR".
My SSL certificate apparently does not cover subdomains of example.com, so perhaps that's the problem? Is there any way to avoid unnecessary SSL processing on the www.example.com domain?
Edit: Problem was that www.example.com did not exist. After creating the sub-domain, the following htaccess code works:
# Map www.example.com to example.com
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
#
# Ensure we are using https:
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
[Still not sure whether the scope of my SSL cert. is an issue.]
Solution was to add the subdomain www.example.com. (See edited question.)

htaccess redirect - I need to redirect subfolder to subdomain

I need to redirect subfolder to subdomain using htaccess
For example:
redirect www.example.com/test (also http://, https://, http://www, https://www)
to
https://user.example.com
how do I do it?
also, how it should be if I need to redirect it to https://user.example.com/test
You can put this code in your htaccess (which has to be in root folder)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule ^test/?$ https://user.example.com/test [R=301,L]
Don't forget to enable mod_rewrite if it's not yet done
Look at original answer at
Can you do a different for blog.example.com?
If you can, just put your Redirect config there:
<VirtualHost *:80>
ServerName blog.example.com
Redirect / http://example.com/blog/
</VirtualHost>
If for some reason you still need to piggy-back on the other vhost, or need to handle all subdomains and not just "blog", then use mod_rewrite:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$
RewriteRule ^/(.*)$ http://example.com/%1/$1 [R=301,L]

.htaccess not working properly while setting SSL redirects only on certain pages

While looking for an answer for the question above I have encountered this script as a solution(I would like to have my website on http except of a few pages like login, register,manage etc.):
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# force https for /login.php and /register.php
RewriteCond %{HTTPS} =off
RewriteRule ^(login|register)\.php$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# don't do anything for images/css/js (leave protocol as is)
RewriteRule \.(gif|jpe?g|png|css|js)$ - [NC,L]
# force http for all other URLs
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !^/(login|register)\.php$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This exact example should work on my server, but it works only partially, meaning, it redirects all pages to http, but I am unable to open login.php or register.php. The webbrowser states that these pages include redirect loop and they can't show up. By no means I am not an expert on mode_rewrite or htaccess so I would appreciate any help.
edit:
I have followed the suggestions and run mywebsite with chrome plugin. I discovered that in the page login.php has redirect loop http->https->http etc. (The only other redirection on that page is when the user is already signed, but it doesn't seem ike a couse for this loop). I have tried also different codes for setting SSL and this one works:
# Rewrite Rules for example.com
RewriteEngine On
RewriteBase /
# Turn SSL on for payments
RewriteCond %{HTTPS} off
RewriteCond %{SCRIPT_FILENAME} \/login\.php [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Turn SSL off everything but payments
RewriteCond %{HTTPS} on
RewriteCond %{SCRIPT_FILENAME} !\/login\.php [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
And this one is working correctly, but it has only one exception for https and I would like to have more of them(around 6). Has someone any idea why the first script is not working? or how to modify the second one in order to have more https websites?
Thanks
I had similar problem, this is what worked for me.
In your httpd.conf, under virtual hosts, make sure you have both:
ServerName domain.com
ServerAlias www.domain.com
BOTH in VirtualHost *:80 AND VirtualHost *:443

Need to redirect all traffic to https

I want to redirect any traffic that goes to http://example.com to https://example.com
same for http://example.com/about to https://example.com/about
I thought it would be something like this:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
This works for me:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
If the traffic is coming in over non-SSL HTTP, then redirect to the HTTP equivalent of whatever page the user was originally trying to access. It also doesn't involve any mod_rewrite options, so it's easy to read.
Side rant: why does everyone feel the need to explicitly set the HTTP code of the redirect and mark one of their rewrites as the "last" one? Seriously, I've seen dozens of same-looking htaccess rules in just the last few days.
This is a previous answer using .httaccess but adding changes proposed in the comments, and some from me:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://my.domain.name%{REQUEST_URI} [L,R=301]
Notes:
This is for the cases where user doesn't have access to main configuration, but has access to .htaccess rules. If you have access to main configuration, use mod_alias solution instead.
For me the rule was not picked up without defining RewriteBase. Explicitly defining it gets rid of ambiguity with some server setups.
At least on some configurations, %{HTTPS} is not set to off when using http, but is null, so !on is more reliable rule than off.
For explicit host name, you don't rely on client side Host header or server configuration. However, explicit host name natually assumes there is only one domain to redirect. Host header poses some considerable problems, such as containing port and being client-supplied data. Another alternative, as suggested by Apache Wiki, is to use %{SERVER_NAME}. If you consider using that, check out caveat from this discussion - it relies on other configuration being correct.
R=301 means it's permanent redirect, as it's usually meant to be in this case. If you instead think it's temporary, that can be left out or specified as R=302.
L means it's last rule to be applied for this request. Leave it if you suspect or know there are other rules after this that you don't want to get applied. You can remove if this is the only rule of the file.
According to the Apache documentation, using mod_alias is more appropriate than mod_rewrite for this task. That is, in order to redirect all HTTP traffic to HTTPS, one would:
<VirtualHost *:80>
ServerName www.example.com
Redirect permanent / https://www.example.com/
</VirtualHost >
<VirtualHost *:443>
ServerName www.example.com
# ... SSL configuration goes here
</VirtualHost >
Two things to note about this configuration:
You need access to the main server configuration file in order for this configuration to work. The VirtualHost directive is only valid in the "server config" context.
Keep in mind that mod_rewrite directives are processed before mod_alias directives. If you've already got a massive block of RewriteRules in your .htaccess file, you might be better off with the mod_rewrite configuration.
why not just plain and simple?
rewriteCond %{HTTPS} !on
rewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
it has worked to me, and seems to me clear. Cheers.
Working in all conditions is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
<IfModule>
After some research this what worked for me, a bit different version.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

301 redirect match problem

Ok I am serving two domains off of one box. The domains are:
www.old.org and www.new.com. As it is now, all of the files and dirs are the same on both domains.
I want to make it so that IF someone goes to www.old.org/folder_a/file_b.php they are 301'ed to www.new.com/folder_a/file_b.php.
I've already tried in the htaccess:
RedirectMatch 301 ^/ http://www.new.com/
But that give a 301 loop because the 301's condition still applies after the 301 is enacted. I think I want to do something that uses rewritecond %{HTTP_HOST} ^.*old.org$ so that only url's at old.org or www.old.org will be affected, but I'm not sure how to do this.
If you have access to the apache vhost configs use those instead of the .htaccess:
<VirtualHost *:80>
ServerName www.old.org
Redirect permanent / http://www.new.com/
</VirtualHost>
If you really must use an .htaccess the following will do:
RewriteEngine On
RewriteCond %{SERVER_NAME} =www.old.org [NC]
RewriteRule (.*) http://www.new.com/$1 [R=301,L]
Some thing like this should do:
RewriteCond %{http_host} ^www\.old\.org$ [NC]
RewriteRule ^/(.*) http://www.new.com/$1 [R=301]

Resources