Forum Moderators: phranque

Message Too Old, No Replies

301 Redirect Question

         

wilkinsj

12:30 am on Dec 14, 2014 (gmt 0)

10+ Year Member



Hi everyone

I need a little help creating a 301 redirect for my site.

My forum is moving from www.example.com/forum/ to www.example.com/forums/. The post URLs are going to be completely different, so I would like all posts in the subfolder /forum/ to redirect to www.example.com/forums/.

In other words, I need all these URLs...
http://www.example.com/forum/
http://www.example.com/forum
http://www.example.com/forum/post

... to redirect to this URL
http://www.example.com/forums/

Is there some way to create a rewrite rule in .htaccess to do this? I have looked everywhere but I can't find anything that will work for this particular situation, i.e. redirecting an entire folder to a single URL.

Thanks in advance for any help!
W

wilkinsj

2:48 pm on Dec 15, 2014 (gmt 0)

10+ Year Member



Anyone have any thoughts on this?

wilderness

4:30 pm on Dec 15, 2014 (gmt 0)

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



Hello,
There are at least three active examples of redirect on the first page of this forum.

Most blogs use strings and you have not provided any such examples?

What have you tried so far?

The forum library [webmasterworld.com] (near page top) also has examples.

wilkinsj

4:50 pm on Dec 15, 2014 (gmt 0)

10+ Year Member



I actually found the answer elsewhere this morning. If anyone else needs it, here it is:

Options +FollowSymLinks
RewriteEngine On
RedirectMatch 301 /forum/(.*) http://www.example.com/forums

lucy24

8:58 pm on Dec 15, 2014 (gmt 0)

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



I actually found the answer elsewhere

Please remove that "elsewhere" site from your bookmarks if the quoted code is representative. What they've given you is two mod_rewrite directives followed by a mod_alias rule-- and a carelessly written one, at that.

phranque

11:33 pm on Dec 15, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I would like all posts in the subfolder /forum/ to redirect to www.example.com/forums/

while this is quite doable i would strongly recommend against it.

this is what google calls a Soft 404 error:
http://support.google.com/webmasters/answer/181708 [support.google.com]
Returning a code other than 404 or 410 for a non-existent page (or redirecting users to another page, such as the homepage, instead of returning a 404) can be problematic.

wilkinsj

11:40 pm on Dec 15, 2014 (gmt 0)

10+ Year Member



Thanks Phranque. I'm aware of this issue. However the alternative is to have around 100,000 hard 404 errors as every single URL on the forum will be changing.

phranque

12:09 am on Dec 16, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



100,000 soft 404 errors will be seen as a signal of low quality.
100,000 hard 404 errors will be seen as a normally function site.

if you have real inbound traffic for those legacy urls you should do the hard work to properly redirect visitors to the new urls.

here are some other alternatives to consider:
- internally rewrite legacy requests to a script that examines the requested url and responds with a 301 status code and the appropriate Location: header referring to the equivalent new url.
- return a 404 status code and specify a custom error document for the 404 status code.
this document would provide appropriate navigation, search, suggested threads, etc to help the visitor reach the requested content.
you could specify this within a <Location> scope to limit the ErrorDocument directive to requests that begin with "/forum".
you could even specify that the error document in this case is /forums/.
this way the search engines see the proper signal (404) and the live visitors see the content you intended (the new forums directory.)

lucy24

2:02 am on Dec 16, 2014 (gmt 0)

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



100,000 hard 404 errors

If it's genuinely impossible to redirect them page-for-page-- which seems unlikely-- you might want to return a 410 instead. It won't affect indexing, but will make the googlebot stop requesting the old URLs sooner.

you could specify this within a <Location> scope

The OP said htaccess, which would preclude <Location>. But if the forums are in a physical directory, you can make a supplementary htaccess within that directory, with its own ErrorDocument directive. Alternatively you could make a nice 410 page that would only apply to requests for the old forums.

wilkinsj

3:49 am on Dec 16, 2014 (gmt 0)

10+ Year Member



Phranque that sounds like a great idea. The old forum uses YAF software, so we could return a 404 to anything beginning with /forum/yaf*, and send those visitors to the new /forums/ page.

If I put an .htaccess file into a /forum/ subfolder, I don't think that will work because Wordpress will deliver its own 404 first right? (forgot to mention that the new site is based on Wordpress, with a BBPress forum). So how do I do it?

phranque

11:36 am on Dec 16, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



If I put an .htaccess file into a /forum/ subfolder, I don't think that will work because Wordpress will deliver its own 404 first right?

not if you prevent those requests from being internally rewritten to the WP script.

wilkinsj

4:26 pm on Dec 16, 2014 (gmt 0)

10+ Year Member



OK so I added an .htaccess file to the /forum/ folder. Its the only file in there. The .htaccess file contains only the following text:
ErrorDocument 404 http://www.example.com/forum-404/

(The page forum-404 is a Wordpress page that I have created with some links to the new forum.)

Now, when I go to www.example/forum/ I get the following error:
Index of /forum
Parent Directory

What am I doing wrong here? Any ideas?

not2easy

4:52 pm on Dec 16, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



In htaccess, when you specify what document to use for a 404 error, don't use the http:// part, just the filename relative to the location of that htaccess file. WP has its own way of handling 404s, there is a page called 404.php in your theme (or in most themes anyway) and that is the page that WP will use so it is best to edit that template and save it as you want the error document to read. Then you don't even need to mess around with a local htaccess file.

Of course that depends on the site structure. If you have other files and another way of handling 404 errors declared in your root htaccess file, I know that WP will use its own 404 page no matter what you have specified, but the WP 404 page would not handle requests for non-WP files. So - IF the whole site will be within WP all you need to do is create a custom 404.php and replace the template file that is in your theme.

To edit that page go to your WP admin panel, select the "Appearance" menu, choose "Theme Editor" and choose the 404 Template. Customize it with the content of the WP page you had created and save it.

Rarely, some custom themes don't have a template. In that case you can make your own. I won't go into that here, as you probably wouldn't need it. Rarely, some themes won't let you save your edit - fix that with file permissions.

lucy24

8:41 pm on Dec 16, 2014 (gmt 0)

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



The .htaccess file contains only the following text:
ErrorDocument 404 http://www.example.com/forum-404/

Two errors. First, an ErrorDocument directive is always root-relative, beginning in / alone. Otherwise it turns into a 302 redirect.
Second, the ErrorDocument has to be an actual, physical file. That means something ending in .html or .php or extension of your choice. And this file has to exist, or you will get an infinite loop.
Now, when I go to www.example/forum/ I get the following error:
Index of /forum
Parent Directory

What am I doing wrong here? Any ideas?

You are missing the line
Options -Indexes

This is completely independent of WP; all sites should include this line. But it isn't present by default on shared hosting. Put it in your general htaccess, the one shared by the whole site. It will be inherited.

WP boilerplate includes the htaccess line
RewriteCond %{REQUEST_URI} !-d

meaning "If this directory physically exists, don't invoke the WP code". But it will save your server work if you add a line before the WP boilerplate to say
RewriteRule ^forum/ - [L]

meaning "Stop right here and don't even try the WP stuff." Other mods will still do their thing. Or, better,
RewriteRule ^forum/ - [G]

wilkinsj

12:03 am on Dec 17, 2014 (gmt 0)

10+ Year Member



Thanks Lucy. I've gone through your recommendations and changed the .htaccess in /forum/ to this:

Options -Indexes
RewriteRule ^forum/ - [L]
ErrorDocument 404 ^/forum-404.php
RewriteCond %{REQUEST_URI} !-d


However, now when I go to example.com/forum or example.com/forum/post, I get redirected to the regular WP 404.php. I would like 404s in this directory to be sent to forum-404.php, which I have just set up in my child theme. Any idea what I'm doing wrong?

phranque

12:38 am on Dec 17, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



ErrorDocument 404 ^/forum-404.php
RewriteCond %{REQUEST_URI} !-d

- the document specified in your ErrorDocument directive should start with a slash, not a caret.
- a RewriteCond directive applies to the following RewriteRule directive.
as it stands, yours is doing nothing.

However, now when I go to example.com/forum or example.com/forum/post, I get redirected to the regular WP 404.php. I would like 404s in this directory to be sent to forum-404.php, which I have just set up in my child theme. Any idea what I'm doing wrong?

the .htaccess in your root directory should be modified so those requests are not being internally rewritten to the WP script.
apache will then look for the requested resource in the filesystem and throw a 404 when the file doesn't exist.

wilkinsj

12:45 am on Dec 17, 2014 (gmt 0)

10+ Year Member



OK now I'm really confused. I used the caret because Lucy24 suggested it, and because I'm trying to redirect from a subfolder to the root. Isn't that right?

Should I just leave out the rewritecond line?

How should I change the .htaccess in the root?

I totally understand that its good to learn about these things, but wouldn't it be easier for someone to just post a little code that I can copy and paste? I feel like we're just going round in circles here and no one wants to offer an actual solution.

lucy24

2:43 am on Dec 17, 2014 (gmt 0)

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



The leading caret is used specifically in mod_rewrite patterns and conditions (and anything else that takes a Regular Expression). Otherwise it is not used.

The special ErrorDocument line cannot go in your general htaccess for the whole site. It has to go in a separate htaccess in the directory that uses it. Remember that the / is always root-relative. So if your custom error document is anywhere else than lying loose in the root directory, you have to say so. For example
ErrorDocument 404 /forum/forum-404.php

or
ErrorDocument 404 /boilerplate/forum-404.php


For your own sanity, keep the directives for each Apache module separate. In particular, don't interlock different mods, as in
Options blahblah
RewriteRule blahblah
ErrorDocument blahblah
RewriteCond blahblah

Each separate mod will still do its thing (I've experimented on this point), ignoring any intervening lines. But really, think of your sanity. mod_rewrite is especially easy to sort out, because all of its directives happen to begin with the element "Rewrite".

The quoted RewriteCond involving %{REQUEST_URI} isn't needed here at all; it is already present in your WP boilerplate. If you keep it-- and if the line "RewriteEngine on" has already occurred-- this orphaned condition will be taken with the next RewriteRule anywhere in the same htaccess, no matter how far away it is. This can lead to what Apache calls Unintended Consequences (euphemism for "The world as you know it may come to an end and don't say we didn't warn you").

wilkinsj

2:56 am on Dec 17, 2014 (gmt 0)

10+ Year Member



Thanks guys but I'm going to hire someone to do this for me. No one seems to have an answer for what I need.