Welcome to WebmasterWorld Guest from 54.198.106.21

Forum Moderators: Ocean10000 & incrediBILL & phranque

.htaccess to redirect /sand and /sandbox to /sandboxes

Redirecting requests for short names to a longer one on the same domain

     
6:42 pm on Jul 16, 2017 (gmt 0)

New User

10+ Year Member

joined:Jan 17, 2004
posts: 17
votes: 0


What do I add to an .htaccess file to redirect requests for example.com/sand and example.com/sandbox (or any URI whose path starts with "/sand") to example.com/sandboxes?

For what itís worth, I believe my current attempt creates an infinite loop:
RewriteRule ^sand(?!boxes).*$ /sandboxes [L]
6:55 pm on July 16, 2017 (gmt 0)

New User

10+ Year Member

joined:Jan 17, 2004
posts: 17
votes: 0


It looks like the same loop problem with RedirectMatch, which doesnít work either:
RedirectMatch 301 ^/sand /sandbox
8:14 pm on July 16, 2017 (gmt 0)

Administrator from US 

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

joined:Dec 27, 2006
posts:3332
votes: 162


Are /sand and /sandbox directories or pages? Is this on WordPress?
8:50 pm on July 16, 2017 (gmt 0)

New User

10+ Year Member

joined:Jan 17, 2004
posts: 17
votes: 0


No directories involved here. /sandbox is a page.

Itís not WordPress, just home†brewed.
9:02 pm on July 16, 2017 (gmt 0)

Senior Member from US 

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

joined:Apr 9, 2011
posts:13841
votes: 485


Don't use RedirectMatch: that's a mod_alias directive, which doesn't play nice with mod_rewrite.

Are you mass-redirecting a lot of URLs to the same one? (Hmmmm.) Or are you consolidating assorted "sandblahblah" directories into a single "sandboxes"? If it's the latter, we would need to take an entirely different approach, so post back with details.

Assuming a few specific URLs:

For what itís worth, I believe my current attempt creates an infinite loop:
RewriteRule ^sand(?!boxes).*$ /sandboxes [L]

You said redirect, didn't you? Then you need the redirect flag [R=301,L] and the target has to include full protocol-plus-domain.

:: detour to test site for experimentation ::

Oh, you can use lookaheads in a RewriteRule. I thought it might be one of those things that only works in a RewriteCond. So your existing first attempt should work fine, with a couple of tweaks:

RewriteRule ^sand(?!boxes) https://www.example.com/sandboxes [R=301,L]
That's assuming you are simply redirecting some pages. If so, your .*$ part isn't needed, since nothing is to be captured.

If you're getting requests for /sandboxes.html or /sandboxes.php or /sandboxesmore-stuff-here, you'll need a closing anchor within the lookahead itself:
^sand(?!boxes$)
10:30 pm on July 16, 2017 (gmt 0)

New User

10+ Year Member

joined:Jan 17, 2004
posts: 17
votes: 0


Thank you!

This works for requests to /sand (even without the "https://www.example.com"):
RewriteRule ^sand(?!boxes) https://www.example.com/sandboxes [R=301,L]

but neither of the following work for /sandboxes.html or /sandboxes.php or /sandboxesmore-stuff-here:
RewriteRule ^sand(?!boxes$) https://www.example.com/sandboxes [R=301,L]
RewriteRule ^sand(?!boxes)$ https://www.example.com/sandboxes [R=301,L]
11:32 pm on July 16, 2017 (gmt 0)

Senior Member from US 

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

joined:Apr 9, 2011
posts:13841
votes: 485


but neither of the following work for

Note the difference in the two. In the first, the closing anchor is within the lookahead, meaning that it is supposed to work on any request for
/sandwhatever
except specifically
/sandboxes (and nothing more)

In the second, the closing anchor is outside the lookahead which in turn means the lookahead is irrelevant, because the rule only applies to requests for
/sand (and nothing more)

even without the "https://www.example.com"

The example.com part is in the target, so it has no effect on whether the pattern is recognized. In targets, you always give the full protocol-plus-domain: http or https, with or without www, depending on your specific site.

When you are testing the rule, replace 301 with 302 (or just say [R,L] without a number) so you don't have to keep refreshing. This is because your browser caches a 301 response, but doesn't cache a 302. ("Oh, this is the same request he made five minutes ago. I remember I got a 301 back then, and 301 means Permanent Redirect, so I'll just proceed directly to the thing I was redirected to five minutes ago.") Then when you're sure of the rule, change the 302 to 301.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members