homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Remove folder from path

 10:43 am on Apr 2, 2012 (gmt 0)

Hi, I am trying to rewrite a URL as follows:

/folder/deleteme/username to /folder/username

I've tried a number of versions. This is what I have so far:

RewriteEngine On
RewriteRule ^folder/([^/]+) folder/deleteme/$1 [L]

Any ideas?



 4:25 pm on Apr 2, 2012 (gmt 0)

Your Rule as written takes the content from, for example,


and serves content from


Are you currently getting a browser error or a 404? (Answer depends on whether there are 301* redirects further along.)

Concrete suggestion: read any 10-12 earlier threads in this Forum, with detour to the horse's mouth [httpd.apache.org].

* Or at least ten, haha, depending on your browser's cutoff.


 7:30 pm on Apr 2, 2012 (gmt 0)

To "remove" something, you need all the following steps:

- link to the right URL from the pages of your site.
- redirect requests for the wrong URL to the right URL.
- rewrite requests for the right URL to the folder where the content really resides.

All three steps are needed.

Use a RewriteRule for the redirect and another RewriteRule for the rewrite.


 2:44 am on Apr 3, 2012 (gmt 0)

Ok, so I have the following rule which works fine, except it doesn't handle a trailing slash.

RewriteRule ^folder/([^/]+)/$ /folder/deleteme/$1/ [L]

I've tried removing slash from the end, but it just redirects back to:


Any ideas?


 2:47 am on Apr 3, 2012 (gmt 0)

Just a note, I want to be able to handle both trailing slash and non trailing slash.


 6:49 am on Apr 3, 2012 (gmt 0)

URLs that end with a slash are folders. The server will redirect a request for example.com/folder to example.com/folder/ automatically.

URLs for pages do not end with a trailing slash. The extension is also optional.

Don't allow two different URLs to show the same content, i.e. with and without slash. This is a duplicate content situation. Use only "without slash" for showing content for pages. If you really have to, redirect "with trailing slash" to "without" for pages.


 9:08 am on Apr 3, 2012 (gmt 0)

I agree with what you have said, but why does the following new rewrite rule:

RewriteRule ^folder/([^/]+)$ /folder/deleteme/$1 [L]

redirect to:


I have removed the trailing slashes, and now when I hit the url "with the trailing slash" on the end it is a 404?


 9:17 am on Apr 3, 2012 (gmt 0)

That rule shouldn't "redirect" at all. With only an [L] flag, it's a rewrite.

Make sure that all of your redirects use RewriteRule syntax with the R and L flag (not Redirect or RedirectMatch) and that all of the redirects are listed before any of your rewrites.


 9:40 am on Apr 3, 2012 (gmt 0)

Sorry, should have been clearer...it is rewriting to that URL. Any ideas why? Is is using a RewriteRule.


 7:48 pm on Apr 3, 2012 (gmt 0)

If folder/deleteme/blahblah is a real folder, then mod_dir (running after mod_rewrite) is stepping in to append the missing slash. It's supposed to; that's it's job.

It isn't enough to get a grip on the difference between rewrite and redirect-- and which direction they go-- although that's important. You also have to keep track of which URLs represent places that really exist on your specific site, because then other rules may kick in. (This is why the boilerplate Rewrites that come packaged with a CMS tend to include the time-consuming !-d and !-f lines.)

Way back in your first post you said you wanted to rewrite

but all examples do the exact opposite, adding /deleteme/ to the middle of the request.

Is this just a glitch in terminology, or an unrelated problem you haven't dealt with yet?


 10:28 pm on Apr 3, 2012 (gmt 0)

A rewrite matches a URL request to an internal filepath.

It does this from the point of view of the requested URL.

So, which one of the two things above is the path part of the URL that was requested, and which one is the path as found in the server filesystem?


 12:52 am on Apr 4, 2012 (gmt 0)

Thanks for the replies guys:

Actual URL found in file system: /folder/deleteme/username/
Desired output URL: /folder/username/

Note: I need the the desired output URL to handle be able to work with both a trailing slash or not.

If you could help me out with how to achieve this, by whatever method, would be appreciated. I would have thought a simple RewriteRule would be possible?


 1:39 am on Apr 4, 2012 (gmt 0)

I need the the desired output URL to handle be able to work with both a trailing slash or not.

Will these URLs be indexed? Since this is a Rewrite, not a Redirect, if you allow two-or-more name formats you will slam straight into Duplicate Content issues.

You started out saying "username". This sounds as if there could be a whole of them, so you can't write a rule that lists all the possibilities. Instead you'll need a rule-- or rather, a Condition-- that works from the other side. You want to exclude requests for the names of actual directories; I assume there aren't many that could be affected.

For the /username bit, decide whether you want it to display with or without trailing slash. Without is generally better-- unless you want your users to feel as if they've each got their own directory-- but you can make a rule either way. Or, if you really and truly don't care, put a /? at the end of the pattern, meaning simply "there may or may not be a slash here".

In the final Rule, make sure your pattern winds up with $ or you'll be rewriting whole directories by mistake :)

Oops, other slightly crucial question. Does the element /deleteme/ currently exist in any URLs anywhere? If so, you have to do the redirect-to-rewrite two-step to intercept anyone asking for the URL in its old form. Yes, you do this even if the URL is right there and working as well as ever.

First Redirect to the without-/deleteme/ version.
Then Rewrite right back to the with-/deleteme/ version.

Do a Forums search if you are getting dizzy. This is a popular question lately.


 7:05 am on Apr 4, 2012 (gmt 0)

Actual URL found in file system: /folder/deleteme/username/
Desired output URL: /folder/username/

mod_rewrite cannot "make" URLs, so your explanation is exactly backwards.

The correct description of what you want to do is this:

* User clicks on link and requests this URL: www.example.com/folder/username/
* Actual *filepath* used in server file system to sevice this request:

The above will be a rewrite.

Of course, you also need a redirect so if user requests
www.example.com/folder/deleteme/username/ they will be externally redirected to www.example.com/folder/username/ and will then make a new request for the new URL.
Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved