I have a site that was hosted by someone else all the web pages were .html files. I am now hosting the site and have changed it to a wordpress site. The domain has not changed but obviously all the pages have. What is the best way to redirect all the .html pages to the main url?
2 Answers
301 Redirect
in an .htaccess
does not require the mod_rewrite library. It's a much simpler way to redirect, but it doesn't have the flexibility and power you get using the Rewrite rules. If you have a 1-1 mapping with explicit urls you can use the Redirect:
Redirect 301 /path/file.html http://new.site.com/newpath.php
If you're trying to do wild card matching of a number of similar patterns using regular expressions you'll need to use Rewrite.
RewriteRule ^(.*).html$ http://new.site.com/$1.php [R=301,NC,L]
Here's a pretty good overview of the 2 methods: http://www.ksl-consulting.co.uk/301-redirect-examples.html
-
1What if I want to redirect WWW and non-WWW version? Can I do this without RewriteRule or mod_rewrite library?– Boris_yoApr 23, 2014 at 6:25
-
@Boris_yo no, you need rewrite conditions if you're trying to catch the protocol http vs. https– RayApr 23, 2014 at 10:52
-
-
1@Boris_yo you can't catch domian / host names either with Redirect, only a RewriteRule with a RewriteCondition:
RewriteCond %{HTTP_HOST} ^foo.com[nc]
RewriteRule ^(.*)$ http://www.foo.com/$1 [r=301,nc]
– RayApr 23, 2014 at 13:16
There is also RedirectMatch that also does wild card matching of similar patterns using regular expressions. The choice depends on just what you need to do.
Rewrite is complex - learning curve - but you can serve alternative urls without giving a HTML code and things that seem imposible. But with great power comes complexity and lots of bugs.
If you are only doing a simple redirection - possibly matching some urls - redirect is the way to go.
When you can't do it with Redirect, you will probably want to start learning Mod_Rewrite.