Forum Moderators: phranque

Message Too Old, No Replies

Wanted: syntax for a 301 redirect for /subdirectory/

in contrast with /subdirectory/filename.ext

         

lavazza

5:47 pm on Nov 15, 2008 (gmt 0)

10+ Year Member



I maintain a site (for a community event) that has been hosted, for a few years, in a subdirectory of a 'commercial' (and niche-unrelated) site

For a variety of reasons, we have (i.e. past tense) relaunched the site on its own domain

I have (successfully) implemented 301s (for just under 100 pages) as follows


Redirect 301 /subdirectory/index.html http://www.example.com/index.html
Redirect 301 /subdirectory/redwidgets.html http://www.example.com/blue-widgets.html
Redirect 301 /subdirectory/greenwidgets.html http://www.example.com/green-widgets.html
Redirect 301 /subdirectory/bluewidgets.html http://www.example.com/blue-widgets.html

etc

However, out there on teh interwebs, there are MANY sites that link to the 'old' sub-directory merely with a trailing slash (i.e. without the index.html)

As almost all of those sites are owned/maintained by (how do I put this politely?) less-than-geeky people, expecting them to edit their links is unrealistic

Anyhoo...

My problem is that I can't figure out how to implement an appropriate 301 for this one, specific use-case

I've tried (in addition to the above)

Redirect 301 /subdirectory/ http://www.example.com/index.html

but that turns out to be (surprise, surprise) a brute-force technique that redirects ALL pages to the new domain's index :(

So... at the moment, anyone who follows such a link ends up at the the old domain's 404 page, which I have edited to include a human-friendly explanatory message... but - to me - this seems to be a rather klutzy workaround

My Google-Fu is weak

Please help!

Thanks in advance

jdMorgan

6:09 pm on Nov 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use the Apache RedirectMatch directive, rather than the Redirect directive.

This allows you to specify "exact", "starts-with", "ends-with", or "contains" matches, as well as general to very-restricted wild-card matches (e.g. based on specifically-defined character-sets), rather than the simple and very-limited prefix-matching supported by the Redirect directive. It also allows for explicitly-specified back-references, rather than the default "carry over anything not specified in the prefix" default back-reference scheme of Redirect.

The directive in this case would be:


RedirectMatch 301 ^/subdirectory/(index\.html)?$ http://www.example.com/index.html

which covers requests for both /subdirectory/ and /subdirectory/index.html

See the Apache mod_alias documentation and the regular-expressions tutorial cited in our Forum Charter for more details.

By the way, I suggest that you *do not* link to or redirect to "index.html" but rather use


RedirectMatch 301 ^/subdirectory/(index\.html)?$ http://www.example.com/

This URL is shorter, easier to type, easier to remember, and is the industry standard "best-practice".

Jim

lavazza

6:43 pm on Nov 15, 2008 (gmt 0)

10+ Year Member



Hi Jim,

THANKS!

It works :)

See the Apache mod_alias documentation and the regular-expressions tutorial cited in our Forum Charter for more details.

For some irrational reason, I'm averse to regular expressions... I guess I simply find them less than intuitive, even though I know they're more elegant

<noteToSelf>
Must

try

harder
</noteToSelf>

Anyhoo...

Thanks, again, for a prompt, concise and coherent reply!

jdMorgan

7:03 pm on Nov 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Elegance has nothing to do with regular expressions' utility. Regular expressions are simply much more powerful than simple prefix-matching or poorly-defined, inconsistent, and limited matching systems (such as were used in MS DOS).

Learning regex is a good investment of time, since they are used in many "languages" -- mod_rewrite, the "Match" flavors of many Apache directives, PHP, PERL, JavaScript, and many, many others.

One thing about regex patterns, though, is that they are *much* easier to write than to read, and easier to read than to fully-interpret from a functional standpoint. This tends to make learning regex very difficult at first, until an epiphany occurs, after which it's much easier.

Now once you learn basic regular expressions, then the elegance comes in; It's one thing to write a regular expression that works, but quite another to make it elegant -- compact, precise, and efficient. :)

Jim

g1smd

9:54 pm on Nov 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Aint that the truth. I'm still waiting for that "moment" on the more complex stuff. :-)