Forum Moderators: phranque

Message Too Old, No Replies

htaccess redirect domain dir to subdomain dir

         

atomMan

3:58 am on Aug 20, 2011 (gmt 0)

10+ Year Member



driving me nuts!

i'm trying to get
domain.com/dir

to redirect to
sub.domain.com/dir

i have my subdomain setup in a separate root directory (so i have public_html and public_html_files, where the latter is the sub)

i'm using this (though i tried many) in the primary domain htaccess...

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} /apps/
RewriteRule ^(.*)$ http://sub.domain.com/apps/$1 [R=301,L]


and every combination i try is sending me to...
http://sub.domain.com/apps/soft/apps/appname/pad.xml


when it should be...
http://sub.domain.com/apps/appname/pad.xml

lucy24

5:10 am on Aug 20, 2011 (gmt 0)

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



Well, the repeated /apps/ part is straightforward. You can't fault the computer for doing exactly what you told it to: add /apps/ to the target for a request that already included /apps/

For example:

Request (captured in full)
/apps/blahblah.html

by your own rule would be rewritten to
http://sub.domain.com/apps/$1

which is to say
http://sub.domain.com/apps/apps/blahblah.html

Now, the "soft" is a mystery to me, so you're going to have to explain that. htaccess doesn't simply invent directories out of thin air :)

The capture can be expressed as
(.+)
rather than
(.*)?
Since you've already specified that it has to include /apps/, there will never be an empty request (that is, domain name alone). And since Regular Expressions are greedy by default, it will capture to the end without further instructions.


Help! Moderators! It would be a big help if forms like "sub.example.com" were also exempt from auto-linking. A lot of questions in the Apache forum involve redirecting among more than one domain.

atomMan

1:39 pm on Aug 20, 2011 (gmt 0)

10+ Year Member



hi lucy24 - thanks for the reply

the current directory structure on the root domain is...
domain.com/soft/apps/[appname]/[filename.ext]

now on my subdomain it is...
sub.domain.com/apps/[appname]/[filename.ext]


so /soft/ has been removed on the sub and i need the former to point to the latter

my directory structure is...
home/usr/public_html
and...
home/usr/public_html_files
where public_html_files is the folder for the subdomain

so if i do what i think you're suggesting, which i've already tried btw :) ...
#RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} /soft/apps/
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L]


i end up at...
http://sub.domain.com/soft/apps/[appname]/[filename.ext]


so the /soft/ isn't getting stripped and i don't know why

i've tried several other combinations and i just can't get it working

i think my problem is with the flags ([R=301,L]), but i don't understand the apache docs well enough to figure this out

wilderness

2:28 pm on Aug 20, 2011 (gmt 0)

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



Help! Moderators!


For the zillionth time, there's only one forum moderator here and despite a few dedicated appearances, he's been absent since late October.

atomMan

4:18 pm on Aug 20, 2011 (gmt 0)

10+ Year Member



also tried these (and several others), all of which produce 500 errors...

RedirectMatch 301 /soft/apps/(.*) http://sub.domain.com/apps/$1
Redirect /soft/apps/.* http://sub.domain.com/apps/$1
RedirectMatch 301 ^/soft/(.*)$ http://sub.domain.com/$1
Redirect /apps http://sub.domain.com/apps

atomMan

4:30 pm on Aug 20, 2011 (gmt 0)

10+ Year Member



i might have found the problem... testing now

g1smd

6:18 pm on Aug 20, 2011 (gmt 0)

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



Don't use Redirect or RedirectMatch. Stick with RewriteRule for all of your rules.

You probably have some rules in the wrong order, and that is exposing the internal path of a previously rewritten request.

atomMan

7:53 pm on Aug 20, 2011 (gmt 0)

10+ Year Member



if i told you what part of the problem was, you'd laugh and i'd be embarrassed :)

so this is what i have, and it works...
RedirectMatch 301 ^/soft/(.*)$ http://sub.domain.com/$1


now that i have it working i don't want to fool around and break everything again on a live server, so if i want to convert that so it uses RewriteRule, what's the best way to go?

and i don't think i want to use the domain name - i should be using the %[SERVER_ROOT] thingy, yeah? or maybe not since i'm redirecting to a subby?

so this would be my attempt...

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} /soft/
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L]


does that look good?

also, what's wrong with using RedirectMatch?

also, i think i'm only interested in an external redirect, but i don't suppose it matters really

g1smd

8:16 pm on Aug 20, 2011 (gmt 0)

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



You'll only need this:

RewriteEngine On
RewriteRule ^soft/(.*) http://sub.domain.com/$1 [R=301,L]


and that code with the [R] in place is an external redirect.

Never mix Redirect or RedirectMatch with RewriteRule as you might just expose previously rewritten internal server paths back out on to the web as new URLs.

lucy24

10:13 pm on Aug 20, 2011 (gmt 0)

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



also, what's wrong with using RedirectMatch?

There is a huge overlap between things that can be done with mod_alias (i.e. Redirect by that name) and mod_rewrite. So if you have anything that requires mod_rewrite, such as a RewriteCond, change everything to mod_rewrite with a [R=301] flag.

At a minimum, it keeps you from making embarrassing errors because mod_rewrite always uses Regular Expressions, while mod_alias only does if you need a RedirectMatch. I'm talking about basics like . versus \. Been there. Done that.

atomMan

5:38 am on Aug 21, 2011 (gmt 0)

10+ Year Member



i thank you very much for the assistance guys - i think i got things squared away pretty well