Forum Moderators: phranque

Message Too Old, No Replies

redirect or rewrite - for the whole domain (help?)

Just asking for help on some .htaccess code

         

caddy303

5:33 pm on Feb 23, 2009 (gmt 0)

10+ Year Member



Hi there. I've read through the posts in this section but still cant find something that works for me.

I'm trying to make sure that any url accessed without ["WWW"...] is rewritten or redirected to the same request WITH the WWW.

mydomain.com = www.mydomain.com
or
mydomain.com/users/login.php =
www.mydomain.com/users/login.php

I've used about 5-6 blocks of code and here is my experience:

I'm able to get the root redirected properly: domain.com to www.domain.com but anything else such as domain.com/users/ will redirect to www.domain.com as well.

I've also used rewrites that has FF tell me it will redirect forever. A potential issue with cookies (Which its not).

I think my problem is that I've installed Joomla and there is already code for Search Engine Friendly URLs. Here it is.

RewriteEngine On
########## Begin - Joomla! core SEF Section
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/¦\.php¦\.html¦\.htm¦\.feed¦\.pdf¦\.raw¦/[^.]*)$ [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
#
########## End - Joomla! core SEF Section

Can anyone help me find a way to make sure that my top level and all trailing levels will go to the "WWW." version of themselves?

jdMorgan

6:02 pm on Feb 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You didn't post your redirect code, and since it may interact with code above, it's impossible to comment.

Jim

caddy303

7:14 pm on Feb 23, 2009 (gmt 0)

10+ Year Member



"You didn't post your redirect code, and since it may interact with code above, it's impossible to comment. "

Didn't know I needed to, I more just need the strings I'm missing to make it work but sure, here are the three blocks I tried.

redirectMatch 301 ^(.*)$ [domain.com...]
redirectMatch permanent ^(.*)$ [domain.com...]

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ [domain.com...] [r=301,nc]

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) [newdomain.com...] [R=301,L]

Thanks for lookin at it

caddy303

7:16 pm on Feb 23, 2009 (gmt 0)

10+ Year Member



Just to make sure I said it properly. Those three blocks are just snippets I found typing "301 redirect" in Google. Its not my code, I've removed it since it doesn't work, and I just need the right code that'll work along side the first posted Joomla code.

Knowing me I didn't explain myself properly, thanks!

g1smd

7:25 pm on Feb 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Anything with Redirect or RedirectMatch in should not be used if you already have other stuff with RewriteRule in it.

Always list redirects before rewrites. If you don't, you'll start exposing internal filepaths back out into URLs, and you don't want that.

Stick to the exact capitalisation used in the Apache manual for RewriteRule, HTTP_HOST, and so on, for the best possible chance of future compatibility.

Note that

^(.*)$
can be simplified to
(.*)
here.

jdMorgan

8:37 pm on Feb 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd suggest:

Options +FollowSymlinks
RewriteEngine on
#
# Externally redirect to canonicalize hostname unless exact match on canonical domain
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# Begin - Joomla! core SEF Section
#
# If requested URL does not resolve to existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# And requested URL-path is not already index.php
RewriteCond %{REQUEST_URI} !^/index.php
# And requested URL-path resolves to a specific filetype or has no filename or filetype
RewriteCond %{REQUEST_URI} (\.php¦\.html?¦\.feed¦\.pdf¦\.raw¦/[^.]*)$ [NC]
# Then internally rewrite to Joomla index.php
RewriteRule (.*) index.php
#
# Unconditionally set HTTP_AUTHORIZATION variable and invoke
# the preceding index.php rewrite (if applicable)
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
#
# End - Joomla! core SEF Section

Note the tweak to the Joomla code: \.html? is equivalent to (\.html¦\.htm), and ^/[^.]*$ already covers "/$" so both of those tweaks get rid of redundancies/inefficiencies.

Replace all broken pipe "¦" characters with solid pipes before use; Posting on this forum modifies the pipe characters.

caddy303

9:01 pm on Feb 23, 2009 (gmt 0)

10+ Year Member



Wow, you're amazing. That worked out perfectly. The whole site turns every domain.com to a www.domain.com, even if you're a few sub directories in.

Genius!

I wont forget WebMasterWorld or you Mr jdMorgan. Thank you so very much!