homepage Welcome to WebmasterWorld Guest from 54.198.140.182
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Htaccess for www won't work with Blog
causes a loop
Lorel




msg:4582795
 1:58 pm on Jun 10, 2013 (gmt 0)

I've been using the following to redirect to the www version of the domain on multiple sites but having a problem with current site where the blog won't load unless I leave it out.

This blog is 2 folders deep which is unusual so I'm wondering if that's the problem (blog/wordpress).

Can anyone see what's wrong with the following.

ErrorDocument 404 /missing.html
AddHandler server-parsed .html

Options +FollowSymLinks +Includes
RewriteEngine On
#
RewriteCond %{HTTP_HOST} !^(www\.example\.net)?$
RewriteRule (.*) http://www.example.net/$1 [R=301,L]

 

phranque




msg:4582807
 2:23 pm on Jun 10, 2013 (gmt 0)

what request did you make and what response did you get?

dougwilson




msg:4582813
 2:59 pm on Jun 10, 2013 (gmt 0)

RewriteCond %{HTTP_HOST} ^mysite.com$ [NC]
RewriteRule ^(.*)$ [mysite.com...] [R=301,L]


The "!" - if not domain then www?

lucy24




msg:4582834
 4:11 pm on Jun 10, 2013 (gmt 0)

RewriteCond %{HTTP_HOST} !^(www\.example\.net)?$

This form is correct for most situations. If you have subdomains, you will need supplemental rules before this final catch-all rule.

dougwilson




msg:4582839
 4:22 pm on Jun 10, 2013 (gmt 0)

without the "!" and the first "www" works for my "this.com/wp/"

lucy24




msg:4582917
 8:21 pm on Jun 10, 2013 (gmt 0)

Yes, sure-- but it won't work for "www.example.com:80" or any of the other possible permutations. The idea is to make a condition that says "If the host is anything other than this precise form" (or precisely nothing if you need to admit HTTP/1.0 users).

having a problem with current site where the blog won't load unless I leave it out

Hard to diagnose without seeing a little more of the htaccess (Noooo! Not the whole thing!) but here's the likeliest guess:

The RewriteRule that creates the domain-name-canonicalization redirect executes after the rule creating the rewrite that is the foundation of your entire blog. This can happen either because the RewriteRules within a single htaccess file are in the wrong order, or because there are multiple layers of htaccess files (VERY perilous if you've got RewriteRules in more than one place).

Lorel




msg:4582925
 8:56 pm on Jun 10, 2013 (gmt 0)

Someone else installed the blog. They removed what I had in there so the includes would work, then added this line and then commented it out for some reason.

# AddHandler application/x-httpd-php5 .php .html .htm

In order for the includes to work I put another addHandler line in there.

Here is the entire htaccess file - it's a small site: All website files are written in html.

ErrorDocument 404 /missing.html
# AddHandler application/x-httpd-php5 .php .html .htm
AddHandler server-parsed .html

Options +FollowSymLinks +Includes
RewriteEngine On
#
RewriteCond %{HTTP_HOST} !^(www\.example\.net)?$
RewriteRule (.*) http://www.example.net/$1 [R=301,L]

@lucy24. Can you explain what you mean by supplemental rules for sub domains. The blog is in a folder not as a sub domain.

lucy24




msg:4582939
 11:05 pm on Jun 10, 2013 (gmt 0)

If you don't have subdomains, don't worry about it. It was a response to the HTTP_HOST issue.

Is that really, literally, your entire htaccess? So your blog files are straight html and their physical location corresponds to the URL? Admirable if so, but, well, I kinda doubt it.

I very strongly suspect that you have an additional htaccess file in the directory that contains wordpress. This in turn means that you almost certainly have conflicting mod_rewrite rules. You have to make special adjustments if you have RewriteRules in more than one place.

Lorel




msg:4583148
 3:26 pm on Jun 11, 2013 (gmt 0)

I will be adding more to htaccess once I get the site working correctly.

There is no htaccess file in the blog folder. I forgot about a mobile folder that someone else set up also and there is no htaccess in that folder either.

Could this line be for the blog and or/mobile? The mobile is not responsive but uses a mobile detect file run by php.

AddHandler application/x-httpd-php5 .php .html .htm

lucy24




msg:4583239
 7:42 pm on Jun 11, 2013 (gmt 0)

Generalization: an AddHandler rule won't stop something from working. In some cases, removing the AddHandler rule will prevent something from working. Well, that's what the rule is for ;)

:: looking vaguely around for someone who speaks wordpress, because I thought the whole thing was built on rewrites and I'm sure my host specifically says somewhere that adding wordpress will overwrite your existing htaccess ::

dougwilson




msg:4583289
 10:46 pm on Jun 11, 2013 (gmt 0)

It may depend on one's host server set up, as far as /wp goes. /wp will follow /htaccess for things like deny from and SetEnvIf instructions but doesn't follow rewrite rules out of the box. At least this has been my experience so far.

I'm not able to remember from one install to the next but I think there is an option somewhere in wp for www or non-www when fresh install is made. Or maybe admin > settings > general ...

and yes there is a htaccess file shipped with every install, I've ever made. It should look something like this:

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

Lorel




msg:4583497
 3:18 pm on Jun 12, 2013 (gmt 0)

Wouldn't the htaccess posted above only apply to the wordpress folder?

I have wordpress installed (by my myself) on about 3 other sites and don't have a problem with the type of Htaccess that I posted above.

I've looked in several folders in the wordpress folder on the current site and don't see an htaccess file.

I checked another site where I installed WP and the htaccess file is in the WP folder (same exact code as posted above) but not with this site.

Should I set up an htaccess file for WP using that code?

lucy24




msg:4583580
 8:16 pm on Jun 12, 2013 (gmt 0)

Wouldn't the htaccess posted above only apply to the wordpress folder?

If it is located in the wordpress folder, sure. BUT there is a huge supplementary problem, because mod_rewrite behaves differently from all other Apache mods.

Now, I'm not going to waste time looking for yesterday's post where I summarized the difference in behavior with/without "RewriteOptions inherit" * but the short version is: Unless you know EXACTLY what you're doing, don't put RewriteRules in more than one htaccess file on the same path.


* Because last time I did this, I (a) searched for ages and couldn't find the post, and then (b) found it had been open in an adjoining tab the whole time. Besides, I kinda think it was split among two different threads.

not2easy




msg:4583585
 8:49 pm on Jun 12, 2013 (gmt 0)

If it is a WordPress install, it creates its own .htaccess (with only that snippet dougwilson posted above) so if one exists in the folder where it is installed, it will be overwritten. It would not overwrite an .htaccess file in the domain's main folder unless it was configured to appear to be installed in the main folder. Ordinarily that small snippet is all that WordPress uses. If it is not showing in that folder with invisible files enabled, then putting one there could be a solution.

Check the Settings on the left menu of your Admin panel, under General, where the name of the blog is set up to see that it is configured to show in the folder it is installed in, then if there is no .htaccess there, put one in with only that snippet posted by dougwilson.

Some host configure things differently, so you might want to ask your host about the issue also. Normally the WP .htaccess works peacefully with any other settings, but configuration settings from within WP itself can alter its behavior.

Lorel




msg:4584475
 4:31 pm on Jun 15, 2013 (gmt 0)

I called the host and they said that htaccess was not necessary in the blog unless I wanted specific changes for the blog which at this point aren't needed.

I checked the General Settings for the blog and the same blog address was set up for both the WP address AND the site address. The Blog is within the site (they are separate) so I removed the blog address from the site address (emptied cache and refreshed the web page) and I still had access to the main blog page via the link from website home page.

However then I uncommented out the www redirect for the website to see if it was working and the link to the blog from website home page quit working again.

Here is the error message I get:

Safari can't open the page
Too many redirects occurred trying to open (the blog)
This might occur if you open a page that is redirected to pen
another page which is then redirected to open the original page.

Here is the current htaccess settings:

ErrorDocument 404 /missing.html
AddHandler application/x-httpd-php5 .php .html .htm

Options +FollowSymLinks +Includes
RewriteEngine On
#
#RewriteCond %{HTTP_HOST} !^(www\.example\.net)?$
#RewriteRule (.*) http://www.example.net/$1 [R=301,L]


PS. I restored the include for application, etc., that were set up originally also and made sure the blog, etc. was still working.

lucy24




msg:4584500
 8:13 pm on Jun 15, 2013 (gmt 0)

Are you sure you didn't accidentally comment-out the condition while leaving the rule? A conditionless

RewriteRule (.*) http://www.example.net/$1 [R=301,L]

would speedily lead to an infinite-redirect loop requiring browser action. This type of problem can only be identified by the browser.

not2easy




msg:4584519
 10:42 pm on Jun 15, 2013 (gmt 0)

I checked the General Settings for the blog and the same blog address was set up for both the WP address AND the site address. The Blog is within the site (they are separate) so I removed the blog address from the site address (emptied cache and refreshed the web page) and I still had access to the main blog page via the link from website home page.

The settings within WordPress General Settings need to be set for the actual location of the blog so it needs to be set to this format: http://example.com/blog-directory-name unless you want it to show up as the main page at example.com. If you subsequently remove all redirects you would have created a conflict with HTTP settings sending example.com traffic to example.com/index.whatever and WP saying that is their address from down in the blog directory.

Commenting out the www (HOST) redirect removed any objection from the domain to WP claiming front page rights. To fix this, uncomment out the HOST redirect, log in to your WP admin and set its location to where it really is. I would worry about trusting your host's advice if they tell you that you don't need to use any .htaccess redirects with a WP install in a directory that is not your site's main directory. The snippet that was posted here is required for WP to properly use a permalink structure and it should be the only thing you would need for an .htaccess file in the directory where your blog is installed.

The www (HOST) redirect belongs in the site's main directory. That and the "index.htm/php" redirects are required in your site's home directory so you can't access (at least) four versions of your site using different URLs.

lucy24




msg:4584525
 11:19 pm on Jun 15, 2013 (gmt 0)

/wp will follow /htaccess for things like deny from and SetEnvIf instructions but doesn't follow rewrite rules out of the box. At least this has been my experience so far.

What you're seeing there is the unique and special behavior of mod_rewrite. You may prefer other adjectives, but these forums have pretty strong word censors. All other Apache mods are inherited from top to bottom (Options -Indexes is one that almost everyone has seen in action) but mod_rewrite isn't. If there are no RewriteRules in the other htaccess files, all is well. But if there are earlier RewriteRules, then the RewriteRule(s) required by WordPress will make it as if any earlier RewriteRules affecting the wp directory simply didn't exist.

There is no completely happy solution. But the safest is to take any RewriteRules that are intended to apply to all directories-- such as index.php and domain-name redirects-- and REPEAT these rules in the appropriate part of the WordPress htaccess.

Lorel




msg:4584992
 4:39 pm on Jun 17, 2013 (gmt 0)

@lucy24 I had commented out both lines of the rewrite. Re your last comment do you mean I need to put the Host rewrite in the blog htaccess?

@Not2easy I have the location of the blog in general settings set up like this (which is where it is located):

http://example.com/blog/wordpress/

I am aware that I need the Host redirect to prevent duplicates.


I went ahead and installed the before mentioned htaccess file in the blog folder, except I renamed it Wordpress as that is the name of the folder it is in (the person who installed it put it in a folder called blog then wordpress inside of that one, i.e., /blog/wordpress/.

I then checked the menu links on website to make sure the blog was still loading then uncommented out both rewrite lines in htaccess for HOST and the link to blog quit working with the error I posted earlier:

Safari can't open the page
Too many redirects occurred trying to open (the blog)
This might occur if you open a page that is redirected to pen
another page which is then redirected to open the original page.

PS every time I made a change to htaccess I cleared the cache and also refreshed the page.

jakebohall




msg:4585002
 5:24 pm on Jun 17, 2013 (gmt 0)

I have a strange set-up on wordpress... not sure if it is similar to your situation, but thought maybe it could help.

For the longest time, I had my main wordpress install at /var/www/html... Later, I wanted to add multiple sites, and I wanted to move my main wordpress to a subfolder.. so I moved to /var/www/html/domain1 ... In order to get things to work, I had to do the following:

Create a .htaccess folder in my main directory that was the standard


# 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


I then put a copy of the index.php folder from /domain1 and changed the require location for loading the wordpress environment..


require('./domain1/wp-blog-header.php');


... in /domain1, I have an htaccess file that redirects to non-www version, cleans up that you can access my site via domain1.com/domain1, and because host allows wildcard.domain1.com. It looks like this:


# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^domain1/ [NC]
RewriteRule ^domain1/(.*)$ http://www.domain1.com/$1 [R=301,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress


Everything seems to be working great for me... Hopefully there is something in here that helps.. (unless I've got everything janky)

dougwilson




msg:4585039
 7:19 pm on Jun 17, 2013 (gmt 0)

After the admin settings we might check the database. Also see wordpress documemtation:

codex(dot)wordpress(dot)org/Changing_The_Site_URL


I've had to do all this in the past when I had similar issues.

Basically everywhere there is an options for wwww or non-www enter the preference

Again, I can't use the "!" with my site set up - don't know why.

not2easy




msg:4585114
 11:27 pm on Jun 17, 2013 (gmt 0)

The .htaccess for your Wordpress install should not have the www HOST redirect, that belongs only in your main folder where your site's files are. The htaccess in the wordprss folder only needs to have the little part that WordPress generates:

# 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>

http://example.com/blog/wordpress/

If your wp-admin, wp-content, etc. folders are in the directory named 'wordpress', then that is where to put the .htaccess file copied here.

Putting the HOST/www. rewrite in the wordpress folder would definitely cause problems, especially if it is repeating rewrites from your main folder. That rewrite only goes in the main public directory for your domain.

lucy24




msg:4585133
 12:44 am on Jun 18, 2013 (gmt 0)

The .htaccess for your Wordpress install should not have the www HOST redirect, that belongs only in your main folder where your site's files are.

Unlike all other Apache behaviors, mod_rewrite is NOT inherited by default. And, just to make sure they get you from both ends, inheritance itself is not inherited either.

So you have to either say the same thing twice or specify "RewriteOptions inherit" in every htaccess file that uses mod_rewrite. Otherwise the results of any earlier RewriteRules will be thrown away as if they had never happened.

This is in the docs, of course, but I have also personally tested it. Even when a RewriteRule in a higher-level htaccess results in a flat-out 403, you can happily go on in if there is subsequent non-inheriting mod_rewrite activity.

Frankly it seems simpler to move everything into your root-level htaccess.

As a last resort you can let your host redirect with/without www requests, though this may lead to duplicate redirects. Most hosts have a clickbox somewhere.

Lorel




msg:4586154
 4:40 pm on Jun 20, 2013 (gmt 0)

I figured out part of the problem. When I uploaded the htaccess file to the blog folder the file had been saved with .txt on the end. Once I removed .txt then the Host redirect worked and also the link to the blog.

If I understand you lucy24 you are saying the Host redirect needs to be in the blog folder also.

After I made the above changes, I noticed the URL from home page to the blog didn't have the www so I tried adding that line from the main folder htaccess and put it in the htaccess for blog folder and tried the page again and it just redirected to the website home page, so I took it back out.

I checked two other sites where I have WP installed and none of them have this problem (the Host redirect works on the website and also the blog) but the blog on those sites aren't in a folder 2 levels deep either.

I"m wondering if reinstalling the blog so it's only 1 folder deep will fix this problem or is there an easier fix?

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved