Welcome to WebmasterWorld Guest from 23.22.46.195

Forum Moderators: Ocean10000 & incrediBILL & phranque

.htaccess conflicts

Main website and subfolder blog conflict

   
2:36 pm on Aug 16, 2012 (gmt 0)

5+ Year Member



I have a website www.example.com with the following in the .htaccess file.

RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://www.example.com/ [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]

This help to append www and ending traling slash automatically if someone does not types it. Everything is fine here.



Now i have a wordpress blog at www.example.com/blog/ with the following in the .htaceesss file

# 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

When i type www.example.com/blog/ everything is fine.

I get errors when i type :
1> www.example.com/blog - does not append the ending traling slash automatically and gives an error page.
2> example.com/blog - does not append the www automatically and gives an error page.
3> All the posts land up to error if www is missed in the url.


How do i solve this ?
7:10 pm on Aug 16, 2012 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Since you have the index redirects after the non-www redirects you have already introduced an unwanted redirection chain when non-www index URLs are requested.

The index.php and index.html rules should be combined into one and listed before the non-www redirect.

You should not be appending a trailing slash for pages. Trailing slash denotes a folder or index page in a folder. See RFC 2616. Remove the slash for pages. Certainly add the slash for folder URLs.

Your index rule works only in the root. Make it work for all folder levels but do not use (.*) for the path detection. It causes multiple backoff and retry pattern matching attempts. Replace it with a more specific pattern.
10:55 pm on Aug 16, 2012 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



Does the content of your new Wordpress htaccess file come before, after or instead of your previously existing htaccess? Order is important.

1> www.example.com/blog - does not append the ending trailing slash automatically and gives an error page.

And there's your Reason #82 for not using the !-f and !-d boilerplate. They're preventing mod_dir from doing its job. It normally executes after* mod_rewrite; one of its two specific jobs is the Directory Slash Redirect.

:: detour to pore over RewriteBase explanation for at least the eleventh time ::

Ah ha!
The RewriteBase directive specifies the URL prefix to be used for per-directory (htaccess) RewriteRule directives that substitute a relative path.

In practice that means: Rewrites that don't begin with a slash (or with full protocol-plus-domain). But since your Rewrites never do begin a slashless relative path, the line does nothing.

RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://www.example.com/ [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]


This is all in the wrong order. Within each category-- F, G, Redirect, Rewrite-- go from most specific to most general. It also illustrates once again why you should leave a blank line after each Rule:

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

RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://www.example.com/ [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]

The first rule makes the second rule superfluous, because you have already redirected everything to www. But I think you must have left out its Condition while copying & pasting, because it would otherwise lead to an infinite (external) redirect loop until the user's browser steps in. And you can combine your two indexes as \.(html|php). In fact there's a post about this same issue just a few threads down.


* I detoured to check. MAMP doesn't go in exactly the same order as my live site, but I'll trust it on this one.
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month