homepage Welcome to WebmasterWorld Guest from 54.145.191.14
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Redirecting all files under /dispatch to Subdomain with /dispatch
htaccess redirect subdirectory to subdomain
gx2000



 
Msg#: 4594036 posted 11:40 pm on Jul 17, 2013 (gmt 0)

I have tried so many variations of this redirect. I need to setup an .htaccess redirect of all files under https://www.company.com/dispatch to https://app.company.com/dispatch. 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/(.*)$ https://app.example.com/dispatch$1 [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]

 

phranque

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



 
Msg#: 4594036 posted 6:13 am on Jul 18, 2013 (gmt 0)

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/(.*)$ https://app.example.com/dispatch$1 [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]

gx2000



 
Msg#: 4594036 posted 1:12 pm on Jul 18, 2013 (gmt 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 https://www.example.com/dispatch/engine?Key=YSaL&ShopNo=225676&Step=View should redirect to https://app.example.com/dispatch/engine?Key=YSaL&ShopNo=225676&Step=View

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"

lucy24

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



 
Msg#: 4594036 posted 9:42 pm on Jul 18, 2013 (gmt 0)

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.

phranque

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



 
Msg#: 4594036 posted 10:13 pm on Jul 18, 2013 (gmt 0)

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.

gx2000



 
Msg#: 4594036 posted 10:32 pm on Jul 18, 2013 (gmt 0)

This worked:

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

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

Thank you for everyone's help!

phranque

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



 
Msg#: 4594036 posted 12:00 am on Jul 19, 2013 (gmt 0)

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.

lucy24

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



 
Msg#: 4594036 posted 4:51 am on Jul 19, 2013 (gmt 0)

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?)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved