Forum Moderators: phranque
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]
"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]
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
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!
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]
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
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!
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
The basic code is usually something like
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
This message:
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
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
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
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!