Apache - redirecting with a new domain - apache

We've had our site running on (say) stuff.com for a while, with a load of sub-domains for different applications, red.stuff.com, blue.stuff.com and of course a load of content stuff.com/things/in/my/head
We've recently purchased a new domain, awesome.com.
Can we configure Apache to replace the 'stuff.com' in any request that turns up, with 'awesome.com' permanently? Would this method keep any search engine listings and existing links valid?
Thanks

You can use Apache's rewrite engine. Add this to your virtual server configuration (either in http.conf or in the sites-enabled directory):
RewriteEngine On
RewriteCond %{HTTP_HOST} stuff.com
RewriteRule ^(.*) %{HTTP_HOST}$1 [C]
RewriteRule ^(.*)stuff.com(.*) http://$1awesome.com$2 [R,L]
The [C] chains the two rules together. The first rule inserts the requested domain name into the request. The next rule redirects the request to the new domain name, extracting the subdomain and the rest of the URL if necessary.
It should redirect any incoming request (I tested it with subdomains, files, and query strings in the URL), so the search engine links would still work. I don't know how it affects search engine ranking, though.

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.)

Apache web server: URLs that contain a "." results in "Permission denied"

We are trying to migrate traffic from our old Netsuite store to a newer solution. The last step before launch is configuring the .htaccess file to redirect traffic from the old Netsuite URLs to the new SEF URLs.
Netsuite URL:
www.myoldstore.com/s.nl/sc.27/.f
New URL:
www.mynewstore.com/store-home-page
For launch, we will point the old domain "www.myoldstore.com" at our new server which is configured to accept traffic for this domain.
I configured the following .htaccess rules:
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} ^www\.myoldstore\.com [NC]
RewriteRule ^ https://www.mynewstore.com/store-home-page [L,R=301]
However, I just discovered that any traffic on our server (Apache 2) that has a path like the old Netsuite URL (/s.nl/sc.27/.f) generates a "Permission denied" message.
Is there some way to configure either the server or .htaccess to allow/handle paths like that?
UPDATE: I found the problem. The new site is a Drupal site, and Drupal's default .htaccess file includees the following rule:
RewriteRule "(^|/)\." - [F]
That is intended to block access to hidden directories. I didn't want to remove it altogether, so I moved it lower in the .htaccess file so it only processes after the specific old URLs have been handled.
The new site is a Drupal site, and Drupal's default .htaccess file includes the following rule:
RewriteRule "(^|/)\." - [F]
That is intended to block access to hidden directories. I didn't want to remove it altogether, so I moved it lower in the .htaccess file so it only processes after the specific old URLs have been handled.

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

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

SEO optimization - front page appears twice?

Web server has initial page in the file, let's say, somefile.html. .htaccess file has the following instruction:
DirectoryIndex somefile.html
which makes web server to fetch contents of somefile.html when web server's root is requested.
However, some page(s) of the website may refer to somefile.html, which contains exactly the same content as server's root. Thus this situation leads to same contents appearing on the web server twice - when somefile.html is requested, and when / is requested.
How to best correct situation with least effort while keeping website structure?
Add a 301 redirect to your htaccess file in your document root (preferably before any rules you may already have:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]*)somefile\.html
RewriteRule ^ /%1 [L,R=301]
This redirects any direct request for /somefile.html to just /. As well as any subdirectories: /foo/bar/somefile.html to /foo/bar/.

Apache Mod Rewrite - Replace www.example.com with www/example.com

First what I have that already works...
On my server I use parked domains where each domain points to the same directory and the software merely determines what the domain is before pulling from the similar named database. For files (images, scripts, etc) I currently have the following setup...
public_html/www.example1.com/
public_html/www.example2.com/
public_html/www.example2.com/
...using the following rewrite...
RewriteCond %{REQUEST_URI} !\.(gif|jpg|js|mp3|png)$
RewriteRule ^[^/]*/(images|search|scripts|sounds)(.+) $1$2 [QSA]
So in example if you request image77.png from example2.com Apache rewrites the (internal) request to...
public_html/www.example2.com/images/image77.png
The external path (in your browser) would simply appear as...
http://www.example2.com/images/image77.png
Secondly, the problem I'm trying to solve...
However as I add more domains this is starting to crowd my *public_html/* directory. What I'm trying to do is simply move all the domain asset directories in to a directory setup that looks like the following...
public_html/www/
...so the www directory would have the following listing...
public_html/www/example1.com/
public_html/www/example2.com/
public_html/www/example2.com/
Lastly, my thoughts on how to possibly approach the problem...
The first Apache variable $1 is generated from %{REQUEST_URI}. If we can simply replace the period . between www and the domain name this should update the rewrite to the newer format though I'm not sure how to do that and keep what works already in tact?
In other words can we can create a rewrite condition to replace just the first period with a slash?
You can capture the domain part after www. and use it later in the RewriteRule as %1
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule ^.*?/(.*)$ /public_html/www/%1/$1 [L]