Solved

Remove a directory from a URL using apache rewrite

Posted on 2008-06-11
7
213 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
Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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