Forum Moderators: phranque
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
If so, then it's likely that the control panel configured the server to put the .net domain's files into a subdirectory of your main site's filespace. If so, then you'll have to put redirect code into a .htaccess file in that subdirectory.
Part of .htaccess implementation is "what code is in it." The other part is, "Where is this .htaccess file located?" The code may be 100% correct, but if it's in a file outside of the scope of the directory tree accessed by the current HTTP request, then that code won't get executed.
Jim
<VirtualHost *:80>
DocumentRoot "/var/www/sites/example/www"
ServerName www.example.com
ServerAlias example.com example.net www.example.net
<Directory "/var/www/html/sites/example/www/">
AllowOverride None
Options All -Indexes
AddType application/x-httpd-php .php .php4 .php3 .phtml .html .htm
AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml text/javascript application/x-javascript application/json
Header append Vary Accept-Encoding
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} !^$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^archive_(.*)_(.*).html$ archive.php?y=$1&m=$2
RewriteRule ^(.*)_category.html$ category.php?catname=$1
RewriteRule ^comments/(.*) comments.php?page_name=$1
RewriteRule ^directory/(.*) dir/directory.php?dname=$1
ErrorDocument 404 /404.php
</Directory>
</VirtualHost>
Also, look into using a more-specific pattern whenever possible to replace ".*" -- and especially when more than one ".*" is used in a pattern (this can cause performance problems due to all of the back-off-and-retry pattern-matching attempts that it causes).
For example, "RewriteRule ^archive_(.*)_(.*).html$ archive.php?y=$1&m=$2" could be better implemented as "RewriteRule ^archive_([^_]+)_([^.]+)\.html$ archive.php?y=$1&m=$2" in most cases. Here we use a negative-match to say, "stop matching when you find an underscore," and "Stop matching when you find a period." Therefore, the URL-path can be matched to the pattern in a single left-to-right pass.
Jim
<VirtualHost *:80>
DocumentRoot "/var/www/sites/example/www/"
ServerName www.example.com
ServerAlias example.com example.net www.example.net
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301,L]
<Directory "/var/www/html/sites/example/www/">
AllowOverride None
Options All -Indexes
AddType application/x-httpd-php .php .php4 .php3 .phtml .html .htm
AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml text/javascript application/x-javascript application/json
Header append Vary Accept-Encoding
RewriteRule ^(.*)_category\.html$ category.php?catname=$1 [L]
RewriteRule ^archive_([^_]+)_([^.]+)\.html$ archive.php?y=$1&m=$2 [L]
RewriteRule ^comments/(.*) comments.php?page_name=$1 [L]
RewriteRule ^directory/(.*) dir/directory.php?dname=$1 [L]
ErrorDocument 404 /404.php
</Directory>
</VirtualHost>
As far as your other suggestion about prefixing the rules with the / because this is in a server config context, I couldn't get it to work that way. When I put the leading / in I would get 400 bad requests for all those requests. I am not sure why it's working this way (maybe due to the fact that I have a trailing slash already on my DocumentRoot and my Directory?
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} !^$
with the string comparison function
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteCond %{HTTP_HOST} !="" When I put the leading / in I would get 400 bad requests for all those requests
As far as your other suggestion about prefixing the rules with the / because this is in a server config context, I couldn't get it to work that way.
RewriteRule ^(.*)_category\.html$ category.php?catname=$1 [L]
RewriteRule ^archive_([^_]+)_([^.]+)\.html$ archive.php?y=$1&m=$2 [L]
RewriteRule ^comments/(.*) comments.php?page_name=$1 [L]
RewriteRule ^directory/(.*) dir/directory.php?dname=$1 [L]
Options All -Indexes
Use
Options All
Options -Indexes AddType application/x-httpd-php .php .php4 .php3 .phtml .html .htm
RewriteRule ^(.*)_category\.html$ category.php?catname=$1 [L]
I thought that by prefixing the period with a backslash in my RewriteRule it would escape the period so it would look for an actual period and not just any character like it appears to be doing now.
Any ideas?
Thanks for the tip about replacing the (.*) with something else. Unfortunately some of the category names have underscores in them so I can't change that rule just yet. I haven't noticed any performance problems but I will keep your advice in mind when I create new sites/pages so that I could name the pages something like a-long-name_category.html.
You could also go with ^([^_]+(_[^_.]+)*)_category\.html$ to solve the trailing-underscore problem.
However, given that the "_category.html" tail is fairly short and that there is only one ".*" sub-pattern in the pattern here, it may not be worth the bother.
Jim