Apache Mod ReWrite to strip folders or files

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.
LVL 1
timbrighamAsked:
Who is Participating?
 
caterham_wwwConnect With a Mentor Commented:
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
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^.* http://%{SERVER_NAME} [R=301,L]

Open in new window

0
 
caterham_wwwCommented:
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

0
 
caterham_wwwCommented:
Forgot to mention... If you intent to drop /foo/bar, remove  "%{REQUEST_URI}" from the substitution.
0
 
timbrighamAuthor Commented:
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

0
 
timbrighamAuthor Commented:
That rule is perfect for what I need - thanks caterham_www!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.