[Webinar] Streamline your web hosting managementRegister Today


Remove a directory from a URL using apache rewrite

Posted on 2008-06-11
Medium Priority
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.

Question by:RegProctor
  • 4
  • 3
LVL 43

Expert Comment

ID: 21768043
You mean this (htaccess assumed)?

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

Author Comment

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.
LVL 43

Expert Comment

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

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]
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions


Author Comment

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.
LVL 43

Expert Comment

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?

Author Comment

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

LVL 43

Accepted Solution

ravenpl earned 2000 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]

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Question has a verified solution.

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

Over the last year I have answered a couple of basic URL rewriting questions several times so I thought I might as well have a stab at: explaining the basics, providing a few useful links and consolidating some of the most common queries into a sing…
If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…
Suggested Courses
Course of the Month8 days, 21 hours left to enroll

590 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