Welcome to WebmasterWorld Guest from 23.20.37.222

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Redirect w/trailing slash

sub-domains

     
3:48 pm on Oct 11, 2013 (gmt 0)

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



Example:

Keyed in URL: http://www.testurl.com/targetdir

Send User to: http://diffsub.testurl.com/targetdir/

I've been working with this code:
RewriteCond %{HTTP_HOST} ^www.testurl.com$ [NC]
RewriteCond /targetdir$
RewriteRule ^http://diffsub.testurl.com/targetdir/ [R=301]


but I'm getting 500s. I'm a tad out of my depth with Apache directives.
6:09 pm on Oct 11, 2013 (gmt 0)

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



Ah. Finally found a solution. Not sure if I need any flags or not but here's what works.


RewriteCond %{HTTP_HOST} ^www\.testurl\.com$
RewriteRule ^targetdir\/?$ http://diffsub.testurl.com/targetdir/
8:39 pm on Oct 11, 2013 (gmt 0)

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



Is /targetdir a real, physical directory or a made-up URL?

You ALWAYS need the [L] flag, unless you have a clear and specific reason to omit it. Any rule that creates a redirect (full protocol-plus-domain in target) also needs the [R=301] flag or it will default to 302. (Same goes for redirects made with mod_alias.) In addition to, not instead, because [R] doesn't automatically imply [L] the way [G] and [F] do.

RewriteCond /targetdir$
<snip>
but I'm getting 500s.

I should think so, unless that was a typo. A RewriteCond requires three pieces and you've only given two. Did you mean %{REQUEST_URI}?
3:51 am on Oct 12, 2013 (gmt 0)

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



you don't need to escape the slashes in Target paths.
assuming .htaccess this would probably do it for you:


RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^targetdir/?$ http://diffsub.example.com/targetdir/ [R=301,L]


if you also want to redirect that path to the new hostname for non-www requests:

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^targetdir/?$ http://diffsub.example.com/targetdir/ [R=301,L]
7:45 pm on Oct 12, 2013 (gmt 0)

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



Thanks for the replies!

@lucy24 - I wasn't aware of the need for 3 pieces of info. I'm a neophyte with Apache directives and reading through the docs is a tad challenging.
8:43 pm on Oct 12, 2013 (gmt 0)

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



Punch line: I entirely overlooked that not only the Condition but the Rule itself was missing a piece:)

RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^targetdir/?$ http:/ /diffsub.example.com/targetdir/

In the Condition: this piece in the middle (generally some aspect of the request) has this value

In the Rule: any request fitting this pattern should instead be sent over here (with one or more flags, depending).

As a general principle: literal slashes only need to be escaped when you're in a tool (such as Javascript or certain Apache mods) that uses /slashes/ to delimit a Regular Expression. Otherwise they have no special meaning and don't require escaping.
1:27 pm on Oct 13, 2013 (gmt 0)

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



:)

So the Condition requires the slash because it can use REGEX but the Rule doesn't? Doesn't the Rule allow for REGEX as well or not?
3:47 pm on Oct 13, 2013 (gmt 0)

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



The pattern you're matching in the condition and the in rule uses RegEx and so needs literal periods and a few other things escaped with slashes.

The rule target is a literal value and should not be escaped.
10:38 pm on Oct 13, 2013 (gmt 0)

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



There are two categories of escaping:

Characters that have special meaning in Regular Expressions (generically) always need to be escaped:
\. literal period (otherwise means "any one character")
\[ literal open bracket (otherwise means beginning-of-group)
\( literal open parenthesis (otherwise means beginning-of-capture or of non-capturing group)
\- literal hyphen inside grouping brackets (sometimes)
\+ \* \? when not used as options/quantifiers
\^ and \$ when not used as anchors
There are others, but those are the ones you see most often.

Characters that have special meaning in a specific function need to be escaped within that function:
\/ in javascript and, er, I forget which Apache mod (not mod_rewrite)
\ (space) everywhere in Apache, because the space has syntactic meaning. Even inside grouping brackets-- which are generally exempt from escaping-- you have to escape spaces, or Apache will think you've moved from one piece of your rule to the next. And you can't use trailing spaces at all. (This is why we have test sites.)
12:02 am on Oct 14, 2013 (gmt 0)

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



http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond
RewriteCond TestString CondPattern
...
CondPattern is the condition pattern, a regular expression which is applied to the current instance of the TestString. TestString is first evaluated, before being matched against CondPattern.

CondPattern is usually a perl compatible regular expression, but there is additional syntax available to perform other useful tests against the Teststring:
...



http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule
RewriteRule Pattern Substitution [flags]
...
Pattern is a perl compatible regular expression. On the first RewriteRule, it is matched against the (%-decoded) URL-path (or file-path, depending on the context) of the request. Subsequent patterns are matched against the output of the last matching RewriteRule.
...
12:20 am on Oct 15, 2013 (gmt 0)

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



Thanks all. Obviously, I better go back to class.
9:30 am on Oct 15, 2013 (gmt 0)

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



This stuff isn't easy.

Post any code here and we'll fix it, tweak it, make it do what you intended.
1:10 am on Oct 17, 2013 (gmt 0)

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



:)
10:32 am on Oct 17, 2013 (gmt 0)

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



But only because you've got "Moderator" after your name. Ordinary civilians get the lecture on Why We Make You Do It Yourself ;)
8:05 pm on Oct 17, 2013 (gmt 0)

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



Number of posts and length of time served counts more than any title. :)
Those that have already contributed in spades are entitled to a little extra help in my book.
11:01 pm on Oct 17, 2013 (gmt 0)

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



By those standards, g1smd can post a query asking what's the URL for amazon, and someone will happily answer :)
12:01 am on Oct 18, 2013 (gmt 0)

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



i'd probably google google for him if he needed the help...
2:14 am on Oct 18, 2013 (gmt 0)

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



@lucy24 - you owe me a beer for that comment about being a mod. :)

But seriously. I do appreciate the offers/support. I think we've all be at some point where a helpful nudge or another pair of eyes was sorely needed and boy did we appreciate the person that came through. It's one of the reasons I continue to hang out here with phranque. Besides the fact that he buys beer for me when we're at PubCon.... #justsayin
 

Featured Threads

Hot Threads This Week

Hot Threads This Month