example.com to www.example.com .htaccess redirect working except one subdir

example.com to www.example.com .htaccess redirect working except one subdir - .htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about apache, htaccess, redirects, 301-redirect, .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :


I've setup a redirect in .htaccess to consolidate example.com to www.example.com traffic on our Apache server.



Looks like this:



Options +FollowSymLinks
RewriteEngine On
RewriteCond %HTTP_HOST ^centerline.net$ [NC]
RewriteRule ^(.*)$ http://www.centerline.net/$1 [R=301,L]


Works as expected, except for the blog.



If I go to http://www.centerline.net/blog, I get the blog fine.
If I go to http://centerline.net/blog, it redirects to the home page (which is the 404 default action).



I tried commenting out the two lines to consolidate centerline.net to www.centerline.net, and I still can't get to the blog via centerline.net/blog.



There's nothing else in the .htaccess related to the blog URL. For the blog, there are several lines that Wordpress inserted into the .htaccess, but commenting those out makes no difference. Just in case, that code is:



# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %REQUEST_FILENAME !-f
RewriteCond %REQUEST_FILENAME !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress


Any ideas where else to look? Or something I'm missing?


Solution :

Here's the relevant snippet from my .htaccess that works for me. I'm going in the opposite direction from you (from http://www.vyznev.net to http://vyznev.net), but the principle should be the same:



#
# URL rewrite engine setup
#
RewriteEngine On
RewriteBase /

#
# Append directory slash (because Apache seems to prepend "www.")
#
RewriteCond /www/asiakkaat/vyznev/public_html/$1 -d
RewriteRule ^(.*[^/])$ http://vyznev.net/$1/ [NS,L,R=permanent]

#
# Canonicalize host name
#
RewriteCond %HTTP_HOST !^vyznev.net$
RewriteCond %HTTP_HOST ^(www.)?vyznev.net$ [NC]
RewriteRule ^(.*) http://vyznev.net/$1 [NS,L,R=permanent]


The first RewriteRule is there just to avoid double redirects for directory URLs without a trailing slash; you probably won't need it, but I included it just in case.



To be honest, I have no idea why the code in your question shouldn't work just as well as mine does. But, in the absence of a better answer, I figured having a known working example might be of at least some help.



Found the issue. The root file had a .htaccess file. Everything in there was fine.



The /blog/ subdirectory though also had an .htaccess file with some duplicate rules. As soon as I renamed that .htaccess file (for all intents and purposes deleted it), http://centerline.net/blog redirected to http://www.centerline.net/blog without issue.


Additionally, if you would like to do some further testing, give the htaccess tester tool a try. It allows you to specify a certain URL as well as the rules you would like to include and then shows which rules were tested, which ones met the criteria, and which ones were executed.

Comments

Popular posts from this blog

Rewrite in Mediawiki, remove index.php, .htaccess

.htaccess rewrite wildcard folder paths from host

Using .htaccess to set a cookie and 301 redirect