Solved

Remove a directory from a URL using apache rewrite

Posted on 2008-06-11
7
211 Views
Last Modified: 2010-07-27
I would like to take any URL's like this:  www.domain.com/site/directory_or_file
and make it:  www.domain.com/directory_or_file

That is, take any URL on my domain and remove "site" if it is a folder at the beginning of the URL.

I already add the w's on the URL with:
  RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
  RewriteRule (.*) http://www.domain.com/$1 [L,R=301]

So you can count on the w's as being there if that helps.

0
Comment
Question by:RegProctor
  • 4
  • 3
7 Comments
 
LVL 43

Expert Comment

by:ravenpl
ID: 21768043
You mean this (htaccess assumed)?

  RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
  RewriteRule ^site/(.*) http://www.domain.com/$1 [L,R=301]
0
 
LVL 1

Author Comment

by:RegProctor
ID: 21768530
I do not understand the question.

All I want to do is remove "/site/" from any URL.

Such as: www.domain.com/site/myfile.html
I want to make: www.domain.com/myfile.html

and: www.domain.com/site/files/myfile.jpg
I want to make: www.domain.com/files/myfile.jpg

and so on.
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 21768745
plysical locatio:n www.domain.com/site/files/myfile.jpg
but user enters in the browser: www.domain.com/files/myfile.jpg
Right?
Then

Options +FollowSymLinks
RewriteEngine On

#if no such file/directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#and it's to our domain specified
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
#and no /site in front of uri
RewriteCond %{REQUEST_URI} !^/site
#rewrite to site/ subdir
RewriteRule .* http://www.domain.com/site%{REQUEST_URI} [L,R=301]
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 1

Author Comment

by:RegProctor
ID: 21769099
Not quite. There is no physical location because these are all dynamically generated pages from a database driven website. Because of this there is no need for checks for directories and files but it's a nice touch and certainly doesn't hurt to do so.

Also, it looks to me like you are doing the opposite to what I want.
Please note, twice I have said I want to REMOVE "site", NOT add it into the URL.
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 21769246
OK, the opposite way
plysical locatio:n www.domain.com/files/myfile.jpg
but user enters(for whatever reason) in the browser the longer version: www.domain.com/site/files/myfile.jpg

That my first rules? Strip the "/site" prefix from URI. Are they're not working?
0
 
LVL 1

Author Comment

by:RegProctor
ID: 21783395
Sorry, I had no Internet yesterday, you mean, this your first rule will do it?:

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^site/(.*) http://www.domain.com/$1 [L,R=301]

I thought it would take something like:

RewriteCond %{REQUEST_URI} ^(/site)(/.*)$ [NC] # look for URL with /site/
RewriteRule ^http://www.domain.com/$2 [L,R=301] # strip it out

0
 
LVL 43

Accepted Solution

by:
ravenpl earned 500 total points
ID: 21784289
> I thought it would take something like:
As usually there's many ways. Yours is wrong, but fixed version

RewriteCond %{REQUEST_URI} ^/site/(.*)$ [NC]
RewriteRule .* http://www.domain.com/%1 [L,R=301]

It's just it can be done within one line
RewriteRule ^site/(.*) http://www.domain.com/$1 [L,R=301]
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In my time as an SEO for the last 2 years and in the questions I have assisted with on here I have always seen the need to redirect from non-www urls to their www versions. For instance redirecting http://domain.com (http://domain.com) to http…
If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

816 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now