Forum Moderators: phranque
I have moved host from a IIS to Apache, and I don't have much experience with htaccess files.
I'm trying to redirect the following url:
http://example.com/index.html
To this url:
http://example.com/
Very simple indeed but when I try the following:
Redirect 301 ^/index.html http://example.com/
Then nothing is happening. I also have access to modrewrite.
Do anyone know how to make this happen ?
Jim
rewritecond %{the_request} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ / [R=301,L]
It does not fix the canonical domain name. The target of a redirect should contain the protocol and domain name.
It only works for the root index file, not for folders. You should also fix the problem in all folders.
You need to write
RewriteCond and THE_REQUEST exactly like that.
# Externally redirect direct client requests for "/index.html" in any directory to "/" in that same directory
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http://www.example.com/$1 [R=301,L]
In addition to g1smd's recommendations, I'll add that it is a very good idea to leave accurate and concise comments in the code, so that you can tell what on earth it was intended to do when you look at it again in five years.
Jim
RewriteRule /tweak/ /articles/ [R=301,L]
RewriteRule /tweak/(.*) /tweak/$1 [R=301,L]
Now after adding these lines to the htaccess, then it doesn't send the index.html request to the Wordpress-engine, but performs a proper redirect.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /tweak/([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ /articles/ [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ / [R=301,L]
Though I would prefere if it just reached the actual index.html and did a 302 html-redirect.
It's fairly certain that you do not *have to* use a redirect to avoid the request being rewritten to WordPress.
Your solution of using a redirect to avoid WordPress is "non-optimal" as it would likely be simple matter to exclude certain subdirectories, hostnames or even filetypes from being rewritten to WP -- The WP code itself would just need a small tweak (one additional RewriteCond) and then you could simply use an internal rewrite for translating your article URLs to filepaths.
It's often a good idea to ask about "the plan" rather than jumping in to asking about code to implement a specific plan. In addition to the actual problem of coding, there are many side-effects: Usability, retaining old inbound links and bookmarks, recovering inbound PR from obsolete URLs, and even preserving your site's "TrustRank" in search engines.
Another point is that the order of rules makes a big difference. In general, you want to put all external redirects first, in order from most-specific pattern (fewest URLs affected) to least-specific pattern (most URLs affected), and then follow them with all internal redirects, again in order from most-specific pattern to least-specific pattern.
This avoids creating situations where multiple redirects are invoked when a URL with mutltiple faults is requested, and also avoids the problem of an internal rewrite being exposed to the client by a subsequent external redirect. It can also prevent unexpected operation, such as the problem of having certain URLs rewritten to a blog or forum script when you don't want them to be.
And finally, it's a very good idea to specify the protocol (http) and the canonical domain in all external redirect rules. Again, this prevents creating opportunities for multiple "chained" or "stacked" redirects to result from a single HTTP request from the client.
Jim
Most Wordpress blogs uses the way of performing non-wordpress redirects first, and then let the rest go to the Wordpress-engine, that can generate custom 404-pages etc.
The whole reason for making this effort is to avoid giving the user the confusing 404 page.