homepage Welcome to WebmasterWorld Guest from 54.161.228.29
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
.htaccess conflicts
Main website and subfolder blog conflict
pemba76




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

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 ?

 

g1smd




msg:4485586
 7:10 pm on Aug 16, 2012 (gmt 0)

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.

lucy24




msg:4485689
 10:55 pm on Aug 16, 2012 (gmt 0)

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.

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