Redirect help from old domain to new domain, using cname www, but use htaccess to exclude specific directories? - apache

We moved our website from our server, domainname.com to another server (a SaaS). We pointed the www via CNAME record in WHM (in order to preserve the MX and other services on the "old" server)
ALL www.domain.com traffic is now sent to the new SaaS. Works great.
For non-www links, we are using:
RewriteCond %{HTTP_HOST} !^(.*)\.domainname\.com$ [NC]
RewriteRule ^(.*)$ http://www.domainname.com/$1 [R=301,L]
and that works great too.
WE ALSO own domain-name.com, and that also lives on the "old" server.
domain-name.com is a misspelling of our company name, and we want all traffic from www and non-www.domain-name to 301 to domainname.com
That is easy too! In the domain-name .htaccess:
RewriteRule .* http://www.domainname.com/ [R=301,L]
Now the fun (and where I could use some help) --
We have many existing links, such as:
www.domainname.com/images/foldername1/image_300x300.png
www.domainname.com/images/foldername2/image_300x300.png
www.domainname.com/html_pages/page1.html
www.domainname.com/html_pages/page2.html
etc... And I'd really like to preserve these without getting sent to the new SaaS and thereby 404ing.
OR -- as an option, the entire contents of www.domainname.com has been cloned to www.domain-name.com -- so we could 301
http://www.domainname.com/images/foldername1/image_300x300.png to
http://www.domain-name.com/images/foldername1/image_300x300.png
and that would be perfect too... Actually, that would be preferred.
I have tried a dozen suggestions from other doing global redirects... but because the primary www.domainname.com is getting its redirect from CNAME, and we MUST trap for non-www in domainname.com's htaccess, HOW can we constrain specific directories (www or not) to 301 to a new server AND also force www.domain-name.com (and non-www) to 301 to www.domainname.com (unless the path is
/images/
/html_pages/
/secret_hiding_place/
Any ideas?
Also have to be careful for 301 loops.
Thanks for any clues or ideas.
THANKS!!!
J

Related

301 Redirect from http to https same page name

checked the Forum but could not find an ideal answer. I have recently installed a SSL Certificate on my site and in the process of creating 301 redirects via the .htaccess file for nearly 400 page urls (to keep Google happy). I thought of using;
redirect 301 /contact.php https://www.mydomainname.co.uk/contact.php
but it breaks the site. The only solution I have seen is;
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^contact\.php$ https://www.mydomainname.co.uk/contact.php [L,R=301]
The above seems a lot of code to use for each of the 400 pages! is there a quicker way with less code I can use in the .htaccess file?
Many thanks. Hope someone can advise.
There are two basic ways of redirecting pages with Apache: Redirect (of mod_alias) and RewriteRule etc. (of mod_rewrite).
Redirect is very simple: it will just redirect a single URL to another. It can be useful sometimes, but it's usefulness is limited to its simplicity: in the case of HTTP-to-HTTPS redirection, it can't differentiate between HTTP and HTTPS connections, so it will just try to redirect to HTTPS even if you're already on HTTPS (and thus you end up in an infinite redirect loop).
RewriteRule, on the other hand, is more advanced and flexible. You can use RewriteCond to conditionally redirect requests; in your case, you'd want to redirect requests only if they're on a HTTP connection.
As you mentioned, you want to redirect to HTTPS for many (I presume all) requests; you can easily do this with only a single rule:
# Enable rewrites
RewriteEngine on
# Only run next RewriteRule on HTTP connections (not HTTPS)
RewriteCond ${HTTPS} off
# Redirect any page to the same URL with https:// schema
RewriteRule (.*) https://${SERVER_NAME}/$1 [L,R=301]
(The ${SERVER_NAME} variable will automatically be equal to your domain name, so you can even use this on web servers with multiple domain names.)

301 Redirect WITHOUT subdirectories/query string

I've been searching for this for hours now. Everything I find does the exact opposite.
I want to 301 redirect ALL traffic from an old domain to a new one. I DON'T want anything to pass across to the new domain, just redirect to the domain itself and nothing else.
I would prefer to do it in a vhost, but htaccess is fine.
In your old domain vhost config (or .htaccess):
RewriteRule .* http://newdomain.com/? [R=301,L]

Apache: Redirect WWW to naked domain

I have an application that was written using Laravel, and it generates dynamic subdomains. Thus, I have a wildcard(*) routing mechanism. Due to several complications I'm having with SSL, I need to redirect the www.example.com routes to the naked domain (example.com), preferrably in the Apache .conf files using RewriteRules.
I've done a few searches and seen a few examples, but most of what I've seen either redirects all subdomains to the naked domain (which breaks the subdomain functionality I stated above) or does the reverse of what I'm trying to do (redirects the naked domain to www) which causes a redirect loop in many of my existing routes.
Is this something that can be done using Apache?
EDIT:
I think I fixed it. For anyone else having similar problems, I used the following rules in /etc/apache2/sites-enabled/000-default.conf:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1$1 [R=301,L]
This redirects any URL that starts with www to its naked equivalent, which has the added benefit of stripping www from subdomains (so, for example, www.test.example.com is redirected to test.example.com which is great (although unnecessary) since www.{subdomain}.example.com is not a defined route in my app and won't work anyways.
I think I fixed it. For anyone else having similar problems, I used the following rules in /etc/apache2/sites-enabled/000-default.conf:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
This redirects any URL that starts with www to its naked equivalent, which has the added benefit of stripping www from subdomains (so, for example, www.test.example.com is redirected to test.example.com which is great (although unnecessary) since www.{subdomain}.example.com is not a defined route in my app and won't work anyways.

Apache rewrite from root folder, force https

I would like to redirect traffic from mysite.com to mysite.com/folder, but also enforce SSL on all pages.
Currently we have this in httpd.conf we found from a sample somewhere, but I don't think that it is working correctly all of the time.
RewriteEngine on
RewriteRule ^/$ https://www.mysite.com/folder/ [R]
The ^/$ only matches if the request is for exactly /, i.e. the root of the site. Anything else is not matched by it. Try .* instead.
Note: if this rewrite rule is active for both the HTTP and the HTTPS version, that'll send you into an infinite redirect loop. In that case, you may need some kind of RewriteCond test, too.

Redirect from one domain to another, without the user realizing it

I have a bunch of domains on on of my servers. I'd like to be able to redirect some domains to a different domain without the user knowing, so not a 301 redirect.
An example, redirect domain.com to masterdomain.com/sites/domain.com. So when visiting domain.com, the user would be displayed with the data on masterdomain.com/sites/domain.com.
Also masterdomain.com and domain.com are on the same server.
How could I do this using Apache? mod_rewritem mod_alias?
If both host names use the same document root, you can do this:
RewriteCond %{HTTP_HOST} =example.com
RewriteRule !^sites/example\.com(/|$) sites/example.com%{REQUEST_URI} [L]
This rule will prepend /sites/example.com if the requested URI path does not already starts with that.
GoDaddy calls this a domain Alais.
Here is a link where it is explained
http://webwizardworks.com/web-design/domain-alias.html