Solved

Remove a directory from a URL using apache rewrite

Posted on 2008-06-11
7
208 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

by:RegProctor
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
> 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PHPmailer and exchange online(Office365) 3 223
php and mysqli 15 28
Apache 2.2 on Windows 2008 32 52
ports for sccm 2012 1 60
Hi, in this article I'm going to teach you how to run your own site, and how to let people in (without IP). I'll talk about and explain each step... :) By the way, everything in this Tutorial is completely free and legal. This article is for …
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

10 Experts available now in Live!

Get 1:1 Help Now