Forum Moderators: phranque

Message Too Old, No Replies

.htaccess redirect subfolder

htaccess redirect subfolder

         

bbing

3:28 am on Feb 25, 2012 (gmt 0)

10+ Year Member



Hi,

I'm having this kind of problem:

When you enter

"http://www.mydomain.com/user1/"
OR
"http://www.mydomain.com/user1"

*Note: public_html/user1/ does not exists that's why I need to redirect them to this

"http://www.mydomain.com/folder/user1/"

Likewise with user2,user3,..., userx
---------------
Is there a way to fix this using .htaccess? thanks!

lucy24

3:59 am on Feb 25, 2012 (gmt 0)

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



You mean, redirect from

/anydirectory/
/(anydirectory/)

to

/folder/anydirectory/
/folder/$1

? Sure. There are, at a rough estimate, 8,000 threads in this Forum talking about how to do it. We don't write your code for you. We make you try it yourself and then someone will step in and tear it to pieces. That's how you learn.

Hint: Make sure the request doesn't already begin with /folder/ or you will end up with

http://www.example.com/folder/folder/folder/folder/..../user1/

bbing

5:05 am on Feb 25, 2012 (gmt 0)

10+ Year Member



Yeah..that's exactly what I meant.

Okay, I'm reading them now :D

Thanks!

g1smd

7:37 am on Feb 25, 2012 (gmt 0)

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



Did you want an external redirect or an internal rewrite?

Both of those can be implemented using a RewriteRule with only a few minor syntax changes.

bbing

8:38 am on Feb 25, 2012 (gmt 0)

10+ Year Member



Hi,

Yes, I need an internal redirect. What are the minor changes? Thanks

bbing

9:30 am on Feb 25, 2012 (gmt 0)

10+ Year Member



Hi,

I tried this code:


Options +FollowSymLinks

RewriteEngine on
RewriteRule ^([^/]+)/([^/]+)?$ http://www.example.com/folder/$1/$2 [R=301,L]

It's working now but only for the http://www.example.com/user1/ and not for the http://www.example.com/user1

g1smd

10:21 am on Feb 25, 2012 (gmt 0)

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



You said you needed an internal redirect? I assume you mean internal rewrite.

You have posted code for an external redirect!

lucy24

12:29 pm on Feb 25, 2012 (gmt 0)

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



It's working now but only for the http://www.example.com/user1/ and not for the http://www.example.com/user1

Good, then it's doing what it's written to do: capture one directory name, one slash, and optionally some further non-slash characters. Your domain name plus following slash don't count.

:: belatedly realizing that my own htaccess does not block anyone from the front page* --not that it matters much in my case ::


* (\.html|/)$ Oops. Slight oversight there. Not sure a closing question mark would have the intended effect.