Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite to remove subdirectory

         

jpl80

4:40 pm on Jan 22, 2009 (gmt 0)

10+ Year Member



All of my files are in:

http://www.example.com/subfolder1/subfolder2/

I want to completely remove subfolder1 from the URL using mod_rewrite. I do NOT want to actually DELETE the folder, I just don't want users to see that it is there in the URL.

How do I do that? Thanks,

[edited by: jdMorgan at 5:01 pm (utc) on Jan. 22, 2009]
[edit reason] example.com [/edit]

jpl80

7:41 pm on Jan 22, 2009 (gmt 0)

10+ Year Member



I may have been thinking about this the wrong way. Perhaps I need to rename 'subfolder1' to 'myfolder' and then move the contents of 'subfolder2' to 'myfolder' and then create the rewrite rules in 'subfolder2' so that there are no 404 errors.

input?

g1smd

7:50 pm on Jan 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need a rewrite, rather than a redirect, to connect the URL request to the actual internal filepath.

This is the job for Mod_Rewrite, exactly what it is intended for.

jpl80

8:22 pm on Jan 22, 2009 (gmt 0)

10+ Year Member



Yes, A rewrite is exactly what I need. What would the syntax be to take user from:

/subfolder1/subfolder2/anyfile.html

to:

/subfolder1/anyfile.html

g1smd

8:34 pm on Jan 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteRule ^pattern-to-match-requested-URL-path /target-server-path [L]

[edited by: jdMorgan at 5:26 pm (utc) on Jan. 23, 2009]
[edit reason] Corrected as noted below. [/edit]

jdMorgan

10:39 pm on Jan 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And to carry-forward the 'filename' :

RewriteRule ^pattern-to-match-requested-URL-directory-path/(.*)$ /target-server-path/$1 [L]

[edit] Correction as noted below. [/edit]

Jim

[edited by: jdMorgan at 5:25 pm (utc) on Jan. 23, 2009]

jpl80

2:14 pm on Jan 23, 2009 (gmt 0)

10+ Year Member



So altogether it should be:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /subfolder1
RewriteCond ^subfolder1/(.*)$ /$1 [L]

jdMorgan

5:25 pm on Jan 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, that should have been be RewriteRule, not RewriteCond. I corrected my post above to prevent further spreading of this error.

No-one has mentioned RewriteBase here, and it should not be needed.

You also will need a RewriteCond to prevent recursion:


Options +FollowSymLinks
RewriteEngine on
#
RewriteCond $1 !^subfolder2/
RewriteCond ^subfolder1/(.*)$ /subfolder1/subfolder2/$1 [L]

This "connects" HTTP requests for the URL /subfolder1/<anything> to the files located at /subfolder1/subfolder2/<anything> on your server.

Once this is installed and tested, change all of the links that appear on your pages to remove "subfolder2/" from the URLs.

An optional third step is to detect client requests for /subfolder1/subfolder2/ URLs, and externally redirect them to /subfolder1/ URLs. This third step is optional, and is not the "fix" for what you are doing here. Changing your on-page links is NOT optional. We can discuss this third step later after you get the first two steps installed, tested, and working.

Jim

g1smd

7:04 pm on Jan 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I doubt that you need the RewriteBase line.

Otherwise it looks good. Check if you need a RewriteCond to prevent looping, as the output might re-match as a new input.

You'll also need a redirect such that should someone directly ask for the old location, their browser is redirected to the new location.

> post delayed 4 hours.

jpl80

7:22 pm on Jan 23, 2009 (gmt 0)

10+ Year Member



need a redirect such that should someone directly ask for the old location, their browser is redirected to the new location

Yes, I definitely need this. Is this a redirect?

RedirectPermanent /(.*) http://www.example.com/$1

g1smd

7:54 pm on Jan 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If the target URL contains a domain name OR the rule contains [R] then it is a redirect.

However, your rule will loop forever. You'll need a RewriteCond as well as perhaps making the .* pattern more specific.

You rule will generate a 302 redirect, so add [R=301,L] to make it a 301. Don't forget that pesky [L]. Always add one of those.

Finally, if you are already using RewriteRule in your file, then make all of your rules using RewriteRule. Mixing Redirect and RedirectMatch in the same file as RewriteRule can have unintended consequences.