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

Apache Web Server Forum

Redirect w/trailing slash

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


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)

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)

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

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)

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)

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)


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)

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)

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)

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:

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)

Thanks all. Obviously, I better go back to class.


 9:30 am on Oct 15, 2013 (gmt 0)

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)



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

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)

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)

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)

i'd probably google google for him if he needed the help...


 2:14 am on Oct 18, 2013 (gmt 0)

@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

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