Hi Steve!
Code:
RewriteEngine on
# redirect from old to new domain will have guaranteed www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+)$ [NC]
RewriteRule ^(.+)$ http://www\.%1/$1 [R=301,L]
# You do NOT escape anything in the redirection - it is NOT regex!
I prefer:
RewriteRule .? http://www.%1%{REQUEST_URI} [R=301,L]
because your :kaioken: EVERYTHING :kaioken: atom is already available as {REQUEST_URI} (and it handles ^/? on its own).
# Redirect old_domain.com to new_domain.com with 301
# Changed regex to (.+), (.?) throws a 500 error?
# Then WHY did you make an atom of it and not use {REQUEST_URI}?
RewriteCond %{HTTP_HOST} ^www\.old_domain\.com$ [NC]
# You need to escape the dot character(s) in regex AND
# {HTTP_HOST} is NOT case sensitive so you need the No Case flag
RewriteRule ^(.+)$ http://www.new_domain.com/$1 [R=301]
Again, no need to capture {REQUEST_UIRI} this way! Instead, use
RewriteRule .? http://www.new_domain.com%{REQUEST_URI} [R=301,L]
# remove .php ONLY if requested directly
# removed the superfluous atom
RewriteCond %{THE_REQUEST} (\.php)
RewriteRule ^([a-zA-Z]+)\.php$ /$1 [R=301,L]
If you feel that you need to ensure nothing after .php, i.e.,
something.php/something_else.php, then use \{space} (NOTE: replace
{space} with a space - too pedantic?).
# Redirect extensionless version to .php version
I'd first check that $1.php exists as a file with
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([a-z]+)$ $1.php [L}
# SYNTAX ERROR/TYPO - we know it's actually ]

Originally Posted by
SS
I am now finding some problems that I did not initially see:
- I can't figure out why typing old_domain.com as a URL I get 403 Forbidden when the rewrite code first ensures that all requests have www enforced and a straight forward redirect?
- I also get a 500 error if I use the ^(.?)$ as the regex for the redirect RewriteRule?
- If I use www.old_domain.com/home then the redirect works. So why does this but the root domain give forbidden?
- Clicking search engine links to new_domain.com do not append www?
- I can only guess that it had something to do with requiring something in the URI in your code - which is precisely why I use any optional character and the {REQUEST_URI} variable.
- It shouldn't ... but no need to use the anchors or create an atom if you use the optional character and {REQUEST_URI} variable.
- Same answer as the first one - you've required at least one character and home is providing four where the simple domain request CANNOT match the requirement for at least one character.
- It takes a while for SEs to catch up.

Originally Posted by
SS
I also tried using the
Code:
RewriteCond %{IS_SUBREQ} false
RewriteRule ^([a-zA-Z]+)\.php$ /$1 [R=301,L]
but it gave me a configuration error.
I suspect it's because {IS_SUBREQ} is only created when it becomes true, i.e., it's returning null. Change the false to !true as, logically, that's the same thing (except for null) and not true is what you're looking for.

Originally Posted by
ServerStorm
Well, after digging in the apache tutorials, I lifted this off their example and it works
Code:
# Using the NULL means that I don't have to explicitly check if it is
# the old domain It matches if it isn't what I specify. Their example
# escaped the periods in the domain name.
RewriteCond %{HTTP_HOST} !^www\.new_domain\.com [NC]
# I think this means don't match line breaks, so I don't think I need it,
# but kept it to preserve their example
RewriteCond %{HTTP_HOST} !^$
# Option use of the /, match anything (surprised they recommend this)
# Probably the noescape (NE) flag is not required as the domain URLs have only [a-zA-Z0-9_]
RewriteRule ^/?(.*)$ http://www.new_domain.com/$1 [L,R=301,NE]
Translation:
If {HTTP_HOST} is not www.new_domain.com and is not empty
(you have a misconfigured server?), optional leading /
(Apache 1.x must be a /; Apache 2.x must NOT be a /;
you should know which you're using!), capture the
{REQUEST_URI} as $1 (why duplicate this?) and redirect
WITHOUT escaping characters (?!?). OMG! What happened?
# Ensure www with 301
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+)$ [NC]
RewriteRule ^(.+)$ http://www\.%1/$1 [R=301,L]
As above
# remove .php ONLY if requested directly
RewriteCond %{THE_REQUEST} (\.php)
RewriteRule ^([a-zA-Z]+)\.php$ /$1 [R=301,L]
As above
# Redirect extensionless version to .php version
RewriteRule ^([a-z]+)$ $1.php [L]
As above
Now I believe that I'm moving forward again

Well, moving forward but you've discovered a few new stumbling blocks as the above indented comments have discussed.
BTW, I am not trying to abuse you! I am simply being my pedantic (and, probably, PITA) self by making sure that you (and anyone else reading this thread) get the full story.
I'm rather surprised, though, that you have not recognized the benefit of .? and %{REQUEST_URI}. .? will always evaluate to TRUE and you have already captured your ^(.*)$ in Apache's {REQUEST_URI} variable. Since Apache handles the leading / problem (between the two major versions of Apache), there is no sense in doing that yourself - an additional benefit. With less work for Apache to do (parse, copy and create a new variable), .? and %{REQUEST_URI} will also be marginally faster because it is far more efficient.
If you're old_domain is a .com and your new_domain is another .com, it would make your handling of {HTTP_HOST} a little easier - at least the part where you could combine the www and non-www treatments and simply capture the new or old domain name and redirect to their www'd versions. Fine as is, though, as the general case should help everyone.
Please ask any questions about any of the above.
Regards,
DK
Bookmarks