Main Content
Forwarding a whole domain, except for a few directories - Apache http server Archive - Originally posted on "The Horse's Mouth" - 2012-09-17 11:02:59 - Graham Ellis
I was set a puzzle via email last week - how to redirect an entire domain to a new server / domain, apart from three specific directories / folders which are to remain on the old server.
Clearly, this is a task for mod_rewrite , and the natural and instrincive reaction is to write a regular expression match to somehow select all domains except those which need to remain on the current server, and to redirect them. Alas, that will proove to be easier said than done. There turns out to be a much easier way to do it ...
1. Rewrite each of the directories that's to be left on the current server, making it a last rule with the [L] option:
RewriteRule ^lv/(.*) lv/ [L]
RewriteRule ^lt/(.*) lt/ [L]
RewriteRule ^ee/(.*) ee/ [L]
2. Then rewrite all remaining URLs to the new server:
RewriteRule (.*) http://newservername.com/
You may choose to add [R=301] on the end of this if you wish to indicate to your visitor that your change is a permanent one.
Some other articles
A607 - Apache httpd mod_rewrite This article Adding a passcode to a directory Telling which ServerAlias your visitor used - useful during merging domains Simplest ever proxy configuration? Finding and diverting image requests from rogue domains How to set up short and meaningfull alternative URLs Redirecting a page - silent, temporary or permanent? Dynamically watching your web site via a PHP wrapper Carrying a long URL around - looking for memorable shorts Redirecting a home page using mod_rewrite Bookkeeping If you have a spelling mistake in your URL / page name mod_rewrite for newcomers Pointing all the web pages in a directory at a database More HowTo diagrams - MySQL, Tomcat and Java Apache httpd, MySQL, PHP - installation procedure What to do if the Home Page is missing Using a MySQL database to control mod_rewrite via PHP Choosing between mod_proxy and mod_rewrite Simple but effective use of mod_rewrite (Apache httpd) Passing GET parameters through Apache mod_rewrite Apache httpd and Apache Tomcat together tips Clustering, load balancing, mod_rewrite and mod_proxy Using different URLs to navigate around a single script Apache httpd to Tomcat - jk v proxy A603 - Further httpd Configuration Identifying and clearing denial of service attacks on your Apache server Helping search engines with appropriate 400 error codes Building up from a small PHP setup to an enterprise one This article Parse error: parse error, unexpected T_STRING on brand new web site - why? Apache Internal Dummy Connection - what is it and what should I do with it? An image from a website that occasionally comes out as hyroglyphics Redirecting a page - silent, temporary or permanent? How did I do THAT? Monitoring and loading tools for testing Apache Tomcat Database connection Pooling, SSL, and command line deployment - httpd and Tomcat Moving a directory on your web site How to avoid duplicating web page maintainance mod_rewrite for newcomers mod_proxy_ajp and mod_proxy_balancer examples Pointing all the web pages in a directory at a database mod_proxy and mod_proxy_ajp - httpd WEB-INF (Tomcat) and .htaccess (httpd) Configuring Apache httpd What to do if the Home Page is missing User and Group settings for Apache httpd web server Strange behaviour of web directory requests without a trailing slash Default file (MiMe types) for Apache httpd and Apache Tomcat Online hotel reservations - Melksham, Wiltshire (near Bath) Which modules are loaded in my Apache httpd Using a MySQL database to control mod_rewrite via PHP Load Balancing with Apache mod_jk (httpd/Tomcat) .php or .html extension? Morally Static Pages Compressing web pages sent out from server. Is it worth it? Simple but effective use of mod_rewrite (Apache httpd) Sharing the load with Apache httpd and perhaps Tomcat httpd.conf or .htaccess? Passing GET parameters through Apache mod_rewrite Clustering, load balancing, mod_rewrite and mod_proxy To list a directory under httpd on a web server, or not? Using different URLs to navigate around a single script An unhelpful error message from Apache httpd Denial of Service ''attack'' Apache httpd to Tomcat - jk v proxy 2006 - Making business a pleasure Apache httpd - serving web documents from different directories Separating 'per instance' data from binaries and web sites Spotting a denial of service attack