Apache Mod ReWrite to strip folders or files

Posted on 2009-02-20
Medium Priority
Last Modified: 2012-05-06
I am familiar with regular expressions for PERL, but for the life of me I can't seem to get these mod rewrite rules correct.  

I need to strip the trailing folders or file names off of any inbound website that matches my virtual host and rewrite it to my main domain name. This includes folders that don't contain a trailing slash.
I've been able to get any files ending in .html or directories terminated with slashes to work, but I need a more general solution.
Question by:timbrigham
  • 3
  • 2
LVL 27

Expert Comment

ID: 23695018
In other words you'd like to redirect http://example.com/foo/bar to http://test.com/ or how does it look like? Do you with so redirect (=external, status code 301 or 302) or to rewrite (internal, transparent)?

If you intend to use .htaccess files: does the main domain point to the same location the other domains are pointing to? Or are you configuring things in the <virtualhost...> container?

Basically a redirect looks like
RewriteEngine on
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} !^(www\.)?maindomain\.com
RewriteRule ^  http://www.maindomain.com%{REQUEST_URI} [R=301,L]

Open in new window

LVL 27

Expert Comment

ID: 23695028
Forgot to mention... If you intent to drop /foo/bar, remove  "%{REQUEST_URI}" from the substitution.

Author Comment

ID: 23695501
I'm using the virtualhost directive in the Apache conf.d directory.

Honestly, I'm not too familiar with the status codes. I know what they are, but I haven't been using them - I've just stuck with the defaults.

Basically, what I am attempting to do is redirect any links that people might have to any of our old websites - say

applicationpool.testdomain.com/application or www.testdomain.com/kit/ or testdomain.com/refer.html  

all to come to the new home page at testdomain.com

I found a couple rewrite rules that almost do what I want (attached), but they are filtering out the images that are supposed to be included on the home page.
ReWriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://%{SERVER_NAME} [R]

Open in new window

LVL 27

Accepted Solution

caterham_www earned 2000 total points
ID: 23695564
301 = moved permanently. This status code is more or less important for search engines, a normal browser wouldn't care.

The conditions which are checking for existing files and folders are only working in per-directory context, you'll need a slight modification for the server context:
RewriteEngine On
RewriteRule ^.* http://%{SERVER_NAME} [R=301,L]

Open in new window


Author Comment

ID: 23695702
That rule is perfect for what I need - thanks caterham_www!

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As Wikipedia explains 'robots.txt' as -- the robot exclusion standard, also known as the Robots Exclusion Protocol or robots.txt protocol, is a convention to prevent cooperating web spiders and other web robots from accessing all or part of a websit…
Introduction As you’re probably aware the HTTP protocol offers basic / weak authentication, which in combination with the relevant configuration on your web server, provides the ability to password protect all or part of your host.  If you were not…
Integration Management Part 2
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month13 days, 18 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question