Getting rid of old .html post extensions but now homepage url gets a trailing "index" at the end
I recently imported an old Blogger into Wordpress (with same domain!).
Everything works fine but i NEED a 301 redirect for SEO and general linking purposes so that all the old posts that had the Blogger structure "www.mysite.com/post-title-truncated.html" lose the .html at the end, and Wordpress will hopefully retrieve that post (sometimes Wordpress is kind enough to guess the post permalink, even when it is slightly different or wrong).
So, as I have a custom post structure, Wordpress already added its own standard rules in my .htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
The first thing I tried to do, to get rid of the old .html in posts permalinks, was this (after Wordpress rules):
RedirectMatch 301 ^(.*)\.html$ http://www.mysite.com/$1
This works beautifully: it gets rid of the old .html from Blogger imported posts, and usually Wordpress guesses the right post even when the (old) permalink is truncated.
The problem? Now it adds "index" at the end of homepage url (which is www.mysite.com/), therefore returning a 404. All other links are fine.
I'm pretty sure it has to do with Wordpress rules but I just can't figure out a way to get rid of .html + make Wordpress find the post even with truncated permalink + not have that bloody "index" tra开发者_StackOverflow中文版iling after www.mysite.com/ homepage url.
Any ideas??
So I solved the issue by doing this:
RewriteRule ^(.*)\.html$ http://www.mysite.com/$1 [R=301,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I appreciate your getting back here to give the solution. There's an issue for those who are on Windows servers, which don't support mod_rewrite, you should use Microsoft URL rewrite module instead.
In your WordPress web.config file, add this rule to the system.webServer
<rewrite>
<rules>
<rule name="Main Rule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php/{R:0}" />
</rule>
</rules>
</rewrite>
精彩评论