Welcome to WebmasterWorld Guest from 54.146.194.42

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Redirecting all files under /dispatch to Subdomain with /dispatch

htaccess redirect subdirectory to subdomain

     
11:40 pm on Jul 17, 2013 (gmt 0)

New User

joined:July 17, 2013
posts: 3
votes: 0


I have tried so many variations of this redirect. I need to setup an .htaccess redirect of all files under [company.com...] to [app.company.com...] I need to do this with about 5 other folders. I have used the following rewrite rules with no success. Can anyone shed some light on what I am doing wrong?

RewriteEngine on
RewriteRule ^dispatch/(.*)$ [app.example.com...] [R=301,L]

AND

RewriteEngine On
RewriteCond %{HTTP_HOST} ^company\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.company\.com$
RewriteRule ^dispatch\/(.*)$ "https\:\/\/app\.company\.com\/dispatch$1" [R=301,L]

AND

RewriteEngine on
#RewriteCond %{HTTP_HOST} ^app\.company\.com\dispatch$
#RewriteCond %{REQUEST_URI} !^app/
#RewriteRule ^(.*)$ /app/$1 [L,QSA]

AND

RewriteEngine on
#RewriteCond %{HTTP_HOST} ^company\.com$ [OR]
#RewriteCond %{HTTP_HOST} ^www\.company\.com$
#RewriteRule ^dispatch\/?(.*)$ "https\:\/\/app\.company\.com\/dispatch$1" [R=301,L]
6:13 am on July 18, 2013 (gmt 0)

Administrator

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

joined:Aug 10, 2004
posts:10542
votes: 8


welcome to WebmasterWorld, gx2000!

with no success

you should be more specific about what request you made, what result you expected, and what response you got.


RewriteEngine on
RewriteRule ^dispatch/(.*)$ [app.example.com...] [R=301,L]

you should either capture the slash after "dispatch" or provide one in the Substitution string.


RewriteEngine On
RewriteCond %{HTTP_HOST} ^company\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.company\.com$
RewriteRule ^dispatch\/(.*)$ "https\:\/\/app\.company\.com\/dispatch$1" [R=301,L]

don't use quotes in the Substitution string and it's not a regular expression so don't escape anything with backslashes.
there's also no need to escape the forward slash with backslash in the Pattern.
see above about providing a slash in the Substitution string.

regarding your OR'ed conditions, i would suggest this:
RewriteCond %{HTTP_HOST} !^(app\.example\.com)?$


the reason for using the ( ) ? in the RewriteCond is to avoid an infinite redirect loop for HTTP/1.0 user agents which don't send a Host: HTTP Request header.


RewriteEngine on
#RewriteCond %{HTTP_HOST} ^app\.company\.com\dispatch$
#RewriteCond %{REQUEST_URI} !^app/
#RewriteRule ^(.*)$ /app/$1 [L,QSA]

(assuming you tried this without comments)
not sure what you're trying to do here.
guessing maybe looking for the canonical hostname before doing an internal rewrite?
you should order your rules so a hostname canonicalization redirect occurs before an internal rewrite in which case you wouldn't need that RewriteCond.
in any case, HTTP_HOST would not include any part of the path.
REQUEST_URI would include all path information so a start-anchored Pattern should have a leading slash.
the QSA flag would only be necessary if you had a query string in the Substitution string and you wanted to append the requested query string.
by default the query string is appended unless you provide a replacement.


also, in this forum, Please Use Example.com For Domain Names in Posts:
http://www.webmasterworld.com/apache/4452736.htm [webmasterworld.com]
1:12 pm on July 18, 2013 (gmt 0)

New User

joined:July 17, 2013
posts: 3
votes: 0


Thank you, phranque.

I'll try to be more specific. We moved a WordPress site away from a proxied server to a VPS and need to redirect files that have been bookmarked or indexed.

For example:
www.example.com/dispatch/anythingthathappenstobehere
to
app.example.com/dispatch/anythingthathappenstobehere

So [example.com...] should redirect to [app.example.com...]

There are hundreds of these dynamic files in Google that need to be redirected. I need it for /app", "/dispatch", "/user", "/shopper_reports", "/networking", and "/articles"
9:42 pm on July 18, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

joined:Apr 9, 2011
posts:12702
votes: 244


Now, wait. Subdomains aren't physical locations. An URL that begins in

app.example.com/blahblah

won't resolve to anything unless there's a rewrite pointing to, probably,

www.example.com/app/blahblah

as in your commented-out rule (here with typo fixes)

RewriteCond %{HTTP_HOST} ^app\.company\.com
RewriteCond %{REQUEST_URI} !^/app/
RewriteRule ^(.*)$ /app/$1 [L]

So before you start wantonly redirecting, make sure you really need to. Are you using new code that wants to make links in a particular form, so it's easier to redirect old requests than to change the form of newly created links?

Or, in the alternative, do you want to go to a subdomain format? That decision should be independent of any server configuration. This is assuming for the sake of discussion that you're not on a low-budget shared-hosting setup where everything is a subdomain of the owner's domain so you've got no choice. It doesn't sound like it.
10:13 pm on July 18, 2013 (gmt 0)

Administrator

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

joined:Aug 10, 2004
posts:10542
votes: 8


RewriteRule ^(.*)$ /app/$1 [L]


So before you start wantonly redirecting...

without an [R] flag or specifying a protocol and hostname in the Substitution string it's not a redirect, it's an internal rewrite.
10:32 pm on July 18, 2013 (gmt 0)

New User

joined:July 17, 2013
posts: 3
votes: 0


This worked:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^app/?(.*)$ [app.example.com...] [R=301,QSA,L]

RewriteCond %{HTTP_HOST} ^example\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^dispatch/?(.*)$ [app.example.com...] [R=301,QSA,L]

Thank you for everyone's help!
12:00 am on July 19, 2013 (gmt 0)

Administrator

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

joined:Aug 10, 2004
posts:10542
votes: 8


you should probably remove the ? in the Patterns.
why is that there?
i'm surprised you aren't getting double slashes in your redirected urls.

reread what i wrote above about QSA flag usage and combining your conditions.
4:51 am on July 19, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

joined:Apr 9, 2011
posts:12702
votes: 244


without an [R] flag or specifying a protocol and hostname in the Substitution string it's not a redirect, it's an internal rewrite.

Can we stipulate that I had an


in the middle of the post? The part I quoted was in reference to a different aspect of the discussion ;)

i'm surprised you aren't getting double slashes in your redirected urls.

Since RegularExpressions are greedy by nature, and work from left to right, they'll always grab the / and put in on the non-captured side. If it were constructed the other way around
(dispatch/?).*$
then the slash, if present, would always be part of the capture.

In fact I think he included the ? specifically to prevent double slashes. I assume the idea was to capture all three of
/dispatch
/dispatch/
/dispatch/morestuff
and append a directory slash if needed, rather than have a potential second redirect if the request was for a directory without trailing slash.

RewriteCond %{HTTP_HOST} ^example\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]


Also expressible as
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]

This is to exclude subdomains, right? You may still want to go with the full-fledged
^((?:www\.)?example\.com)?$
if you've got http 1.0 customers. (Uh... how does 1.0 request a subdomain?)
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members