Forum Moderators: phranque

Message Too Old, No Replies

subdomain _only_ works with extra trailling slash (.com//)

hosts have odd config?

         

jazzle

9:08 am on Sep 6, 2005 (gmt 0)

10+ Year Member



I currently have the following as my .htaccess:


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

jdMorgan

1:51 pm on Sep 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jazzle,

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

using a local url-path only. See the warnings about this in the ErrorDocument documentation [httpd.apache.org].

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]

Note that posting on this board modifies the pipe "¦" character used above. You'll need to replace it with a solid pipe from your keyboard before use.

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]


In fact, those will all produce 302 redirects, plus a warning in your error log if your server is set to LogLevel Warn.

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]

There's no need to include [A-Z] in the pattern, since you've specified a case-insensitive compare by using [NC]. Also, you should "root" the substitution URL with a leading slash as shown whenever possible. Testing the HTTP_HOST prevents this rule from affecting your subdomains.

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

jazzle

2:41 pm on Sep 6, 2005 (gmt 0)

10+ Year Member



wowzers - didn't realise I'd asked such a complex question - thank you for taking the time to answer it! :D

Will have to take your suggestions and see how I go, though I imagine you will have got it spot on.

thanks again, j

jazzle

3:30 pm on Sep 6, 2005 (gmt 0)

10+ Year Member



Hi,
I sense that I'm nearly there, but I now get Redirection Limit errors on the subdomains. (I think this must be something to do with the host's setup since I have another site with a different host, and the same thing works nearly* fine.)

* 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?

jazzle

4:22 pm on Sep 6, 2005 (gmt 0)

10+ Year Member



Hmm, now I really am confused - the subdomains are no longer working on my second site.

TIA, j

jdMorgan

12:30 am on Sep 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can you please be very specific about what URL you request, what the result is (error messages, wrong page, what? Anything in the server error log?), and what code snippet you expect to implement/affect that rewrite function, and where that code is located in your directory structure.

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

jazzle

7:07 am on Sep 7, 2005 (gmt 0)

10+ Year Member



I have found that I can use the Redirect page on cPanel instead. (Trying to do it myself was creating a loop).

Thanks for all your help earlier with correct formatting of my file.