Forum Moderators: phranque
ErrorDocument 401 http://example.com/401
ErrorDocument 403 http://example.com/403
ErrorDocument 404 http://example.com/404
ErrorDocument 500 http://example.com/500### prevent external access to this file
<Files .htaccess>
order allow,deny
deny from all
</Files>### no indexes
Options -Indexes### rewrites...
RewriteEngine On## remove www.
RewriteCond %{HTTP_HOST} ^www?\.example\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www?\.example\.net$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www?\.example\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]# /html [old location]
Redirect /html/ http://example.com/ [R=301,NC,L]# /forums > forums.ex...
Redirect /forums [forums.example.com...] [R=301,L]# /blog > blog.ex...
Redirect /blog [blog.example.com...] [R=301,L]## now the science bit...
RewriteRule ^([A-Za-z0-9]+)/?$?page=$1 [NC]
Hopefully that's self-explanitory, so I'll just add a couple of notes:
• As it stands, my main site uses the last rule and some php to use pretty-URLs (Search Engine Friendly), and I must keep this.
• /forums & /blog should simply intercept those strings before the last rule is applied, but it seems the [L] is ignored as the result URL is [blog.example.com...]
• [blog.example.com...] => 404
but [blog.example.com...] works fine!?
If you have _any_ ideas/thoughts/corrections, please do let me know - I will be most grateful.
j
Welcome to WebmasterWorld!
Actually, you have an even worse problem, and that is that your site will return a 302 redirect for any 403/404/500 error page. You can confirm that here [webmasterworld.com] by requesting a non-existent or forbidden page.
The correct syntax for the ErrorDocument directive is:
ErrorDocument 404 /404.html
I strongly suggest that you use static pages for errordocuments. Otherwise, a minor script error can cause catastrophic failure of your server, and leave you with no error pages to diagnose the problem.
Your canonical domain redirects will also fail if a port number is appended. I'd suggest:
## remove www.
RewriteCond %{HTTP_HOST} ^www\.example\.(net¦com¦co\.uk) [NC,OR]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
The following three directives are malformed, being hybrids of the Redirect directive from mod_alias [httpd.apache.org], and the RewriteRule directive of mod_rewrite [httpd.apache.org]. They won't work as expected, because Redirect does not support the use of flags such as [R=301] or [L].
# /html [old location]
Redirect /html/ http://example.com/ [R=301,NC,L]# /forums > forums.ex...
Redirect /forums http://forums.example.com [R=301,L]
# /blog > blog.ex...
Redirect /blog http://blog.example.com [R=301,L]
A proper coding would be:
# /html [old location]
RewriteRule ^html(/.*)?$ http://example.com$1 [R=301,NC,L]# /forums > forums.ex...
RewriteRule ^forums(/.*)?$ http://forums.example.com$1 [R=301,L]
# /blog > blog.ex...
RewriteRule ^blog(/.*)?$ http://blog.example.com$1 [R=301,L]
And now the science bit:
## now the science bit...
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^([a-z0-9]+)/?$ /?page=$1 [NC,L]
Another cause for your unexpected problems with the [L] flag is that mod_rewrite in .htaccess appears to be recursive. That is, [L] stops mod_rewrite processing for this pass through the code only. However, once rewriterules are applied in .htaccess, the server must re-run httpd.conf and any .htaccess files in the new filepath. This must be done so that the server can apply any other rewrites or access restrictions that apply to the new URL-path.
So, your mod_rewrite code will be re-invoked until no further rewriterules match. Using the [L] flag is still a very good idea though, since it causes mod_rewrite to exit 'early' on each pass, and prevents rules later in the file from being applied to URLs that have already been rewritten earlier in the file.
Jim
* On the other site I have a peculiar behaviour; The correct spelling (scc) doesn't redirect (and doesn't remove the www. either), yet others work as intended.
# /scc > scc.ex...
RewriteRule ^scc(/.*)?$ [scc.example.co.uk$1...] [R=301,L]
# + other attempts seen
RewriteRule ^(ssc¦sk.*)(/.*)?$ [scc.example.co.uk$2...] [R=301,L]
Any ideas about either of these problems?
The more detailed you get about these factors, the more likely we can help. (Imagine that the person who knows the answer has 10 minutes --and 10 minutes only-- per day to help.)
Jim