Forum Moderators: phranque

Message Too Old, No Replies

I need to get rid of or make one directory level invisible

mod_rewrite question

         

Trisha

8:55 pm on Jan 8, 2007 (gmt 0)

10+ Year Member



(sorry, title should have been 'mod_rewrite question')

I'm using some software that is making my urls like:

h ttp://subdomain.mydomain.com/extra-directory/section1

I would like the urls to be like:

h ttp://subdomain.mydomain.com/section1
h ttp://subdomain.mydomain.com/section2
etc.

I don't think I can install the software in the root of the subdomain because I believe it refers back to the directory somewhere in the script.

The 'extra-directory' is kind of long and not a familiar word to most people.

I tried:

RewriteRule ^(.*)? extra-directory/$1 [L]

But it isn't working, I get a Error 404 - Not Found when trying to go to
h ttp://subdomain.mydomain.com/section1

It seems like this should be easy, but mod_rewrite always confuses me. I just muddle my way through it most of the time.

[edited by: Trisha at 8:57 pm (utc) on Jan. 8, 2007]

jdMorgan

9:27 pm on Jan 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you mean that the software is making links appear on your pages like http://subdomain.mydomain.com/extra-directory/section1
then mod_rewrite can't help you. That's the link that users will see, and that their browser will request when users click the link, unless you change the script that generates the page.

As a result, the rule


RewriteRule (.*) extra-directory/$1 [L]

will only serve to rewrite that URL, once requested, to
http://subdomain.mydomain.com/extra-directory/extra-directory/section1 -- two levels deep!

If you reverse the rewrite, to:


RewriteRule ^extra-directory/(.*)$ /$1 [L]

then the server will attempt to serve the /extra-directory-prefixed pages from your Web root.

So, neither mod_rewrite solution will work. If you want to change a link, it has be changed on the page that the visitor is looking at. You can then rewrite the requested URL, which is for a page in your Web root, back to the /extra-directory level where it actually resides. Of course, these on-page URLs must be sufficiently unique for mod_rewrite to determine that they need to be rewritten. Otherwise, it would rewrite *all* pages requested from your Web root to the /extra-directory level. :(

Jim

Trisha

7:58 pm on Jan 12, 2007 (gmt 0)

10+ Year Member



Thanks Jim! I've been feeling pretty stupid! The software didn't add real obvious links, but after I read your reply and dug around a bit I found that it was adding link further into the site which used the longer url. In the end I found a different way to install it so that I didn't need mod_rewrite - but I feel pretty stupid for not recognizing that wouldn't work myself!

Thanks again