Change domain redirect all URLs exactly except one URL that must be redirect to a different URL - apache

Change domain from example.net to example.com.
I must to redirect all URLs exactly except one URL that must be redirected to different URL.
To redirect all URLs exactly (www and non-www) I use:
RewriteEngine On
RewriteCond %{HTTP_HOST} (w*)example\.net$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,R=301]
Till here everything is perfect.
But I need one URL from old domain to be redirected to a different URL:
example.net/en to be redirected to example.com/abc instead of example.com/en.

I got the fix here: https://webmasters.stackexchange.com/questions/128426/redirect-all-urls-exactly-except-one-url-that-must-be-redirected-to-a-different/128427#128427
I do not understand why on #StackOverFlow I get negative vote for this question.
I hope to help other users this fix!

Related

Apache2 301 Redirect Not Working

I have attempted to read various documentation, as well as other answers online, and I cannot find a solution to my problem.
Here is my issue:
I have a permanent www to non-www redirect setup to point traffic from www.domain.com to domain.com
This works, but I am having issues when trying to visit specific urls. For example:
We have three PDFS that we would like people to be able to download via:
domain.com/paper/somepdfname.pdf
When i visit the url like this it works, but if I try to visit something like this:
www.domain.com/paper/somepdfname.pdf
The browser gets redirected, and strips one of the slashes out, resulting in "the site cannot be reached error". This is the url I end up with in the browser:
domain.compaper/somepdfname.pdf
I think its pretty clear that I need to somehow make sure there is a forward slash put before the paper, but I do not know how to do this. Below you will find my .htaccess directive for handling the redirect:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1/ [R=301,L]

redirect exact url to another url

There are a bazillion examples online of doing redirects via apache's htaccess, but I can't find any example of redirecting a full URL match to another full URL.
For instance, I have an existing website at
https://example.com
How do I redirect some specific URLs for that domain to a different one:
https://example.com/login --> https://my.example.com/login
https://example.com/register --> https://my.example.com/register
For every other every other path on example.com I need to remain untouched so my site still works fine (i.e. /blog shouldn't redirect somewhere else).
You can use the following :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(login|register)/?$ http://my.example.com/$1 [L,R]
This will redirect example.com/login or example.com/register to http://my.example.com/
You can alternatively accomplish this using the RedirectMatch directive (if the newurl is on a diffrent webserver) :
RedirectMatch ^/(login|register)/?$ http://my.example.com/$1

htaccess Mod_Rewrite from Main Domain to SubDomain

I have heard this thread (.htaccess redirect all pages to new domain) very informative. However, my request is a bit different from this original request.
Above thread talks about redirecting from old domain to a new domain, however, I have requested to only forward main domain to a subdomain.
Here is a question - How do I automatically forward all the request from my main domain to subdomain. For example -
Landing URL: http://example.com
Forwarding URL: http://sub.example.com
Landing URL: http://example.com?var=foo
Forwarding URL: http://sub.example.com?var=foo
If someone lands on my forwarding URL, I do not want them to be again redirected with 301. In simple words, I do not want to use a rule which always redirects all the request which lands on http://sub.example.com if the landing URL is the same.
If I follow the advice from the above link, I believe that will happen in my case as I want to forward from the main domain to a subdomain.
Here is what I have so far and it is doing partial job. It only forwards my one domain to another subdomain. However, it does not work when there are any URL parameters, it just stays the same old URL.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^$ http://sub.example.com/? [R=301,NE,NC,L]
In simple words - my code does not react when there is http://example.com?var=foo and it remains as it is.
Any guidance would be great.
You need to remove a condition and change your regex.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)/?$ http://sub.example.com/$1 [R=301,NE,NC,L]
Clear your browser cache then give that a try. Let me know how it works.

Apache Redirect to static location

I need to do a redirect on any url from one domain to a one static url on another domain
example:
http://myexample.com -> redirect to http://example.myexample.com
http://myexample.com/example -> redirect to http://example.myexample.com
I have seen a lot of example where the redirect carries over the /example to the redirect site.
I DO NOT want this...I need any urls from one sight regardless of parameters to redirect to:
http://example.myexample.com
This should be straightforward, see below:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^myexample.com$
RewriteRule ^ http://example.myexample.com/? [R,L]
This will redirect anything hitting myexample.com regardless of the path to http://example.myexample.com/

htaccess rewrite rule to redirect specific page on specific domain to another url

I'm hosting 2 sites on the same server with different domains. I'd like to set up a redirect for a specific page on domain 1 and have it go to another external url. Have searched and searched and experimented but can't get it work.
The nearest I've got is
RewriteRule ^oldpagename$ http://externalsite/page [R=301,L]
but the problem with this is that it catches that url on the other domain as well. How can I restrict it to a specific domain?
thanks
You need to add a condition to match against the variable `%{HTTP_HOST}:
RewriteCond %{HTTP_HOST} ^(www\.)?domain1.com$ [NC]
RewriteRule ^oldpagename$ http://externalsite/page [R=301,L]