homepage Welcome to WebmasterWorld Guest from 54.198.130.203
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
How to remove a directory using mod rewrite (.htaccess)
i want to remove the directory folder
hippypink




msg:3389123
 11:16 pm on Jul 8, 2007 (gmt 0)

here is an example of current site:
http://www.example.com/directory/filename.html

i want to remove the directory folder so that it works like this:
http://www.example.com/filename.html

I have spent well over an entire day trying many things, including running the rewritelog, which didn't give any clear insights (I tried over 30 different things). any ideas?

if it helps, here my purpose: basically, I prefer to keep lots of my files organized on the web-server in different directories, while keeping everything appearing at the base level of the website for SEO reasons--prominence of filename in url)

thanks in advance!

[edited by: jdMorgan at 11:39 pm (utc) on July 8, 2007]
[edit reason] example.com, no sigs, please. [/edit]

 

jdMorgan




msg:3389140
 11:40 pm on Jul 8, 2007 (gmt 0)

See this recent thread, started yesterday: [webmasterworld.com...]

Jim

hippypink




msg:3401589
 1:48 am on Jul 23, 2007 (gmt 0)

I gone through it several times, and tried several things, but I think he is doing something a little different.

Let me put in a list of the things I have tried:

###### NONE OF THESE WORK #########

# RewriteRule ^/(.+)/?$ /articles/$1.html [L]

# RewriteRule ^http://www.mysite\.com\/(.*) [mysite.com...] [R=301,L]

# RewriteRule ^articles/$ [%{HTTP_HOST}...] [R=301,L]

# RewriteRule ^(.*)$ index.php?title=$1 [QSA]

# RewriteRule ^(.*)$ [%{HTTP_HOST}...] [L]

# RewriteCond ^http://%{HTTP_HOST}/(.*)
# RewriteRule [%{HTTP_HOST}...] [L]

# RewriteRule ^/articles/$ / [R]

# RewriteCond ^/articles/(.*)
# RewriteRule [%{HTTP_HOST}...]

(of course I realize these are all commented out)

g1smd




msg:3401978
 1:06 pm on Jul 23, 2007 (gmt 0)

# Redirect a direct request for any /articles URL back to root to avoid Duplicate Content (also fixes domain as www).
RewriteCond %{REQUEST_URI} ^articles [NC]
RewriteRule ^articles/(.*)$
http://www.main-site.com/$1 [R=301,L]

# Redirect any non-www request to www to avoid Duplicate Content problems.
RewriteCond %{HTTP_HOST} ^main-site\.com [NC]
RewriteRule ^(.*)$
http://www.main-site.com/$1 [R=301,L]

# Silently rewrite a request that is not for an /articles URL to the articles folder (www has already been fixed by previous rule).
RewriteCond %{REQUEST_URI} !(.*)articles
RewriteRule ^(.*)$ /articles/$1 [L]

hippypink




msg:3402819
 7:27 am on Jul 24, 2007 (gmt 0)

no good, but I give up--its no longer worth the effort. but thanks g1smd. I see what you are trying to do however, I have more than one sub directory on my site.

best solution: rewrite each file something like this:

RewriteRule ^http://www.my-site.com/1.html$ [my-site.com...]

(of course this doesnt work, but you get the point--I give up, really!)

g1smd




msg:3403400
 6:59 pm on Jul 24, 2007 (gmt 0)

Ah, for multiple subdirectories, you need to be a bit more creative.

The "virtual" filename in the root folder needs to have some part to its filename that suggests what the folder name is going to be.

Then you need one set of rules like those above for each folder that you have.

Lets say that the filename the user sees has aaa- or bbb- etc prepended, and that the actual folder names are /111/ and /222/ etc.

These names can be whatever you want them to be, you decide what they are and make a list somewhere to refer to as you modify the code:

# == REDIRECTS TO PROTECT FOLDER NAMES FROM BEING INDEXED ==

# Redirect a direct request for any /nnn folder URL back to root to avoid Duplicate Content (also fixes domain as www).

RewriteCond %{REQUEST_URI} ^111 [NC]
RewriteRule ^111/(.*)$ http://www.main-site.com/aaa-$1 [R=301,L]

RewriteCond %{REQUEST_URI} ^222 [NC]
RewriteRule ^222/(.*)$ http://www.main-site.com/bbb-$1 [R=301,L]

RewriteCond %{REQUEST_URI} ^333 [NC]
RewriteRule ^333/(.*)$ http://www.main-site.com/ccc-$1 [R=301,L]

# == REDIRECT ALL NON-WWW REQUESTS TO WWW ==

# Redirect any non-www request to www to avoid Duplicate Content problems.

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

# == REWRITE BASE URLS TO FOLDERS ==

# Silently rewrite a request that is not for a /nnn URL to the nnn folder (www has already been fixed by previous rule).

RewriteCond %{REQUEST_URI}!(.*)111
RewriteRule ^aaa-(.*)$ /111/$1 [L]

RewriteCond %{REQUEST_URI}!(.*)222
RewriteRule ^bbb-(.*)$ /222/$1 [L]

RewriteCond %{REQUEST_URI}!(.*)333
RewriteRule ^ccc-(.*)$ /333/$1 [L]

The prepended words and the folder names should each all be different.

This may not be the most efficient way to do things but it should get you started. However, it is much more efficient than having one redirect per file. That is NOT scalable or easy to maintain.

g1smd




msg:3403442
 7:30 pm on Jul 24, 2007 (gmt 0)

If you are prepared to allow the whole folder names to be prepended exactly "as is" to the base URLs then that code can be simplified to just half a dozen lines.

The more complicated example, above, allows for the prepended word to not be the same as the real folder name, it allows you to tailor the URL the visitor sees to have whatever you want in it, irrespective of the underlying folder name it is rewritten to.

g1smd




msg:3403562
 9:15 pm on Jul 24, 2007 (gmt 0)

WARNING. There is an error in above code.

There MUST be a SPACE before each ! in the code above.

Add it each time.

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