Solved

Remove a directory from a URL using apache rewrite

Posted on 2008-06-11
7
209 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

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.

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 you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

27 Experts available now in Live!

Get 1:1 Help Now