Forum Moderators: phranque

Message Too Old, No Replies

How do i 301 redirecti/subfolder/index.php to /subfolder/

Redirecting subfolder/index.php to /subfolder/

         

reggy

11:30 am on Jun 24, 2009 (gmt 0)

10+ Year Member



Hello,
I have been trying to redirect my WP subfolder to go from /subfolder/index.php to /subfolder/ and also have the non-www version of the subfolder to go to the www version - An example of the code i used is below - it worked fine but it caused all my posts to give a 404 error - Can anyone figure out what i am doing wrong?

I have tried this on different sites on 4 different servers, so its a problem with the code

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com/subfolder/ [NC]
RewriteRule ^(.*)$ http://www.example.com/subfolder/$1 [L,R=301]

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.example.com/subfolder/ [R=301,L]

[edited by: jdMorgan at 1:07 pm (utc) on June 24, 2009]
[edit reason] example.com [/edit]

jdMorgan

1:06 pm on Jun 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are trying to match server variables against values that they will never match. For example, "HTTP_HOST" will never contain any part of the URL-path, since it only contains the hostname (possibly in FQDN format), and perhaps an appended port number.

"RewriteEngine on" is only required once -- at the beginning of the mod_rewrite code.

"Options +FollowSymLinks" --if needed at all on this host-- is similarly only required once.

"RewriteBase /" is the default value, and is not needed unless a different RewriteBase has been previously set.

The two rules here are in the wrong order -- put the most specific rules first, so that only one redirect is invoked, even if "/subfolder/index.php" is requested from the non-canonical domain.

The regex pattern for "THE_REQUEST" is too ambiguous, and will be slow to match.

Literal periods in the regex patterns should be escaped.

I'd suggest a review of the mod_rewrite documentation at Apache.org -- Cutting and pasting code from forums is a dangerous practice, as any errors or use of inappropriate code can put you out of business. That said, I'd suggest:


Options +FollowSymLinks
RewriteEngine on
#
# Externally redirect direct client requests for "/subfolder/index.php" to "subfolder/"
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /subfolder/index\.php[#?]?
RewriteRule ^subfolder/index\.php$ http://www.example.com/subfolder/ [R=301,L]
#
# Externally redirect to canonical "www" hostname (for /subfolder requests only)
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^subfolder/(.*)$ http://www.example.com/subfolder/$1 [R=301,L]

This leaves three questions, the first being, "Why are you redirecting to a subfolder to begin with?"

The next question is, "Have you updated all of your on-page links to point to the correct URLs, so that you won't force a redirect every time a client (including search robots) requests a WP page?" (This will force the client to make two requests for every single page, slowing them down, costing you extra bandwidth, and 'polluting' your stats snd making them questionably-useful).

Finally, why are you only canonicalizing the index page requests and domain name requests for the subfolder requests, instead of for all requests?"

The whole 'plan' here seems inconsistent, and may result in non-optimal search ranking side effects. It makes me think that the requirements specification stage was rushed-through, and you've proceeded too soon to coding.

Jim

reggy

1:33 pm on Jun 24, 2009 (gmt 0)

10+ Year Member



Hi Jim,
Thanks for your response! I think i didnt make my self very clear.

I already have my homepage which is a standard html page - I then put a blog into a subfolder.

The homepage redirects fine for both non-www to www and also /index.html to non-index.html

The problem i was having was trying to do the same with the subfolder

If i am correct, its ok to have more than one .htaccess file right? One in the root and one in the sub-folder?

That is what i have - the main issue i was getting with the subfolder was getting /subfolder/index.php to redirect to /subfolder/ and when i tried out that code i gave you it worked well but caused all my posts to give 404 errors.

So would the code you gave me match what i want to do?

Thank You!

reggy

1:43 pm on Jun 24, 2009 (gmt 0)

10+ Year Member



Jim,
I did try your code out and it didnt work - should the code go into the .htaccess in the root or the subfolder?

jdMorgan

3:14 pm on Jun 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you use multiple .htaccess files, I strongly suggest putting all redirects into the root .htaccess file. Otherwise you run the risk of having an internally-rewritten filepath 'exposed' to clients as a URL.

I would suggest using the following in example.com/.htaccess:


Options +FollowSymLinks
RewriteEngine on
#
# Externally redirect direct client requests for "<any directory>/index.php",
# "<any directory>/index.html" or "<any directory>/index.htm" to "<any directory>/"
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index\.(php¦html?)[#?]?
RewriteRule ^(([^/]+/)*)index\.(php¦html?)$ http://www.example.com/$1 [R=301,L]
#
# Externally redirect non-canonical hostname requests to canonical "www" hostname
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

Replace all broken pipe "¦" characters with solid pipe characters before use; Posting on this forum modifies the pipe characters.

Any subsequent problems will likely be a result of unexpected interaction with the internal rewrite rule used by WP itself, incorrect installation of WP, or of linking to "index.anything" from within your own site (which is an SEO no-no).

If you have problems, please try to be much more descriptive, instead of just saying "it doesn't work" (Try that with a car mechanic, and you won't much like his bill).

How did you test (e.g. what did you type)?
What were the results?
How did those results differ from your expectations?
What (if anything) did you find in the server error log file?

Jim

reggy

6:43 pm on Jun 24, 2009 (gmt 0)

10+ Year Member



Hi Jim,
I have deleted the .htaccess from the subfolder and just included the last code you gave me in the .htaccess in the root ( replacing the old redirect code)

Now everything works fine except that the pages/posts on my blog still give me the following error code below:

Not Found

The requested URL /subfolder/post-name/ was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

Apache/2.2.11 (Unix) mod_ssl/2.2.11 OpenSSL/0.9.8i DAV/2 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 Server at www.example.com Port 80

I get the following message when i try to click on any post!

jdMorgan

11:06 pm on Jun 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's what you see in your browser window, but what's in your server error log file?

I don't believe I'd just delete the code in the subfolder .htaccess file unless it was only the code previously-discussed here. WP requires a RewriteRule that it provides or installs in order to function, as far as I know.

Jim

g1smd

11:44 pm on Jun 24, 2009 (gmt 0)

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



You'll have to add in the WP rewrites, and place them after the redirects that you just installed.

jdMorgan

3:07 am on Jun 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think the WP rewrites go into /subfolder/.htaccess if I'm understanding the current set-up correctly.

The basic code is usually something like


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

... or something like that.

This message:

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

means that a custom 404 error document has been declared for your site or for your subdirectory, but that custom error document does not exist.

This declaration may be in the server configuration file (and likely put there by an option in your Control Panel if you cannot edit your server config files yourself), or you will find a directive in one or more of your .htaccess files like


ErrorDocument 404 /path-to-custom-404-error-page.html

Note that the path shown is a local file path, not a URL! If a URL is used, the result is an almost-SEO-fatal 302-Found redirect response instead of a 404-Not Found response.

Anyway, in your reported case, this custom error document does not exist at the specified file-path. So, you'll want to either remove the ErrorDocument declaration, correct the specified ErrorDocument filepath, or create a 404 error page at the specified file-path.

Jim

reggy

6:27 pm on Jul 4, 2009 (gmt 0)

10+ Year Member



Hi JMorgan,
My host couldnt help me out with this as it was beyond their support.

I discovered that the 404 error was because of the permalinks - which were RIGHT, but just by saving them again, it corrected the 404 errors and the posts now display!

I just need a rewrite code to be able to redirect example.com/folder/index.php to example.com/folder/ - everything else seems to work fine!

Thank You!

g1smd

7:30 am on Jul 6, 2009 (gmt 0)

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



If you used the code supplied above, then it should already be doing that, as can be seen from the code comment:

# "<any directory>/index.html" or "<any directory>/index.htm" to "<any directory>/"

reggy

10:25 pm on Jul 8, 2009 (gmt 0)

10+ Year Member



jdMorgan and g1smd - this has worked fine now

Thank You!