Forum Moderators: phranque
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 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
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
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/
Jim
THANKS!
It works :)
See the Apache mod_alias documentation and the regular-expressions tutorial cited in our Forum Charter for more details.
<noteToSelf>
Must
try
harder
</noteToSelf>
Anyhoo...
Thanks, again, for a prompt, concise and coherent reply!
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