Welcome to WebmasterWorld Guest from 54.166.220.138

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

How to remove a directory using mod rewrite (.htaccess)

i want to remove the directory folder

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

5+ Year Member



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]

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

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

Jim

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

5+ Year Member



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)

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

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



# 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]

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

5+ Year Member



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!)

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

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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.

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

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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.

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

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



WARNING. There is an error in above code.

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

Add it each time.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month