Forum Moderators: phranque

Message Too Old, No Replies

Wordpress .htaccess

         

thosecars82

2:33 pm on Dec 16, 2009 (gmt 0)

10+ Year Member



Hello
I have a problem with the format of wordpress pages. Firstly, I did not know what was causing the problem.
<snip>
The .htaccess file from the root, that is to say,www.example.com is the one causing the problem because when I removed that file the format of the blog's pages was correct. The thing is that i need that .htaccess file for te rest of the site. So,
how do you suggest modifying the .htaccess file to let the blog pages display properly when you browse
www.example.com/blog?
Thanks a lot

[edited by: jdMorgan at 2:47 pm (utc) on Dec. 16, 2009]
[edit reason] No URLs, please. See Terms of Service and Charter. [/edit]

thosecars82

5:13 pm on Dec 16, 2009 (gmt 0)

10+ Year Member



Sorry for this long post. Here it is the .htaccess which I do not have idea so far as for where the error might be.

DirectoryIndex intro.php

RewriteEngine on
RewriteBase /
#begin adding an ending slash for (intro¦home¦services¦advice¦webdesign¦clases¦contact¦writeEmail¦aboutme¦notfound)/language/(en¦sp)
RewriteRule ^(intro¦home¦services¦advice¦webdesign¦clases¦contact¦writeEmail¦aboutme¦notfound)/language/(sp¦en)$ http://www.example.com/$1/language/$2/? [L,R=301]
#end adding an ending slash for (intro¦home¦services¦advice¦webdesign¦clases¦contact¦writeEmail¦aboutme¦notfound)/language/(en¦sp)

#Begin delete old dynamic link from google index with a redirect R = 301. Si solo se pone R, se pone 302 por defecto
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^language=sp$
RewriteRule .* http://www.example.com/? [L,R=301]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^language=en$
RewriteRule .* http://www.example.com/intro/language/en/? [L,R=301]
#End delete old dynamic link from google index with a redirect R = 301. Si solo se pone R, se pone 302 por defecto

#Begin Loop Stopping Code
RewriteCond %{REQUEST_URI} ^/(stats/¦missing\.html¦failed_auth\.html¦error/).* [NC]
RewriteRule .* - [L]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
#End Loop Stopping Code

#Begin replacing www.example.com/home.php with www.example.com/home/language/sp/ to avoid aliases which is bad for SEO. To make this work I had to place the code for avoiding infinite loops
RewriteRule (intro¦home¦services¦advice¦webdesign¦clases¦contact¦writeEmail¦aboutme¦notfound)\.php$ $1 [NC,R=301]
#End replacing www.example.com/home.php with www.example.com/home/language/sp/ to avoid aliases which is bad for SEO. To make this work I had to place the code for avoiding infinite loops

#Begin remove the .php from www.example.com/home.php/.*

RewriteCond %{REQUEST_URI} !^/(intro¦home¦services¦advice¦webdesign¦clases¦contact¦writeEmail¦aboutme¦notfound)\.php/language/en/? [NC]
RewriteCond %{QUERY_STRING} !^language=en$
RewriteCond %{REQUEST_URI} ^/(intro¦home¦services¦advice¦webdesign¦clases¦contact¦writeEmail¦aboutme¦notfound)\.php/.*$ [NC]
RewriteRule .* http://www.example.com/%1/language/sp/? [L,R=301,NC]

RewriteCond %{REQUEST_URI} !^/(intro¦home¦services¦advice¦webdesign¦clases¦contact¦writeEmail¦aboutme¦notfound)\.php/language/en/? [NC]
RewriteCond %{QUERY_STRING} ^language=en$
RewriteCond %{REQUEST_URI} ^/(intro¦home¦services¦advice¦webdesign¦clases¦contact¦writeEmail¦aboutme¦notfound)\.php/.*$ [NC]
RewriteRule .* http://www.example.com/%1/language/en/? [L,R=301,NC]

#End remove the .php from www.example.com/home.php/.*

#Begin remove the .php from www.example.com/home.php/language/en/
RewriteCond %{REQUEST_URI} (intro¦home¦services¦advice¦webdesign¦clases¦contact¦writeEmail¦aboutme¦notfound)\.php/language/en/?$ [NC]
RewriteRule .* http://www.example.com/%1/language/en/? [L,R=301,NC]
#End remove the .php from www.example.com/home.php/language/en/

#Begin redirect intro/language/sp e intro/language e intro/ e intro a www.example.com
RewriteCond %{REQUEST_URI} (/intro/.*)¦(/intro)[NC]
RewriteCond %{REQUEST_URI} !/intro/language/en/?$[NC]
RewriteRule .* http://www.example.com/? [L,R=301,NC]
#End redirect intro/language/sp e intro/language e intro/ e intro a www.example.com

#begin redirect example.com/home into example.com/home/language/sp
RewriteCond %{REQUEST_URI} /(home¦services¦advice¦webdesign¦clases¦contact¦writeEmail¦aboutme¦notfound)/?$ [NC]
RewriteCond %{QUERY_STRING} !^language=en$
RewriteRule .* http://www.example.com/%1/language/sp/? [L,NC,R=301]

RewriteCond %{QUERY_STRING} ^language=en$
RewriteCond %{REQUEST_URI} /(home¦services¦advice¦webdesign¦clases¦contact¦writeEmail¦aboutme¦notfound)/?$ [NC]
RewriteRule .* http://www.example.com/%1/language/en/? [L,NC,R=301]

#end redirect example.com/home into example.com/home/language/sp

#remove the language part of the end of the url when this language is not available and therefore, not accepted
RewriteCond %{REQUEST_URI} (intro¦home¦services¦advice¦webdesign¦clases¦contact¦writeEmail¦aboutme¦notfound)/language/(.*)$ [NC]
RewriteCond %{REQUEST_URI} !(intro¦home¦services¦advice¦webdesign¦clases¦contact¦writeEmail¦aboutme¦notfound)/language/sp/?$ [NC]
RewriteCond %{REQUEST_URI} !(intro¦home¦services¦advice¦webdesign¦clases¦contact¦writeEmail¦aboutme¦notfound)/language/en/?$ [NC]
RewriteRule .* http://www.example.com/%1/ [L,R=301,NC]
#remove the language part of the end of the url when this language is not available and therefore, not accepted

#begin add www to the url so that the url name is cannonical. In this case we must not remove the query string
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#end add www to the url so that the url name is cannonical. In this case we must not remove the query string

#begin convert dynamic urls into static urls
RewriteRule ^(intro¦home¦services¦advice¦webdesign¦clases¦contact¦writeEmail¦aboutme¦notfound)/language/([^/]*) $1.php?language=$2 [L,NC]
RewriteRule ^(intro¦home¦services¦advice¦webdesign¦clases¦contact¦writeEmail¦aboutme¦notfound)/language/([^/]+)/ $1.php?language=$2 [L,NC]
#RewriteRule ^(intro¦home¦services¦advice¦webdesign¦clases¦contact¦writeEmail¦aboutme¦notfound) $1.php #this line is not needed any more since we are adding slash at the end of the url in case it is missing
RewriteRule ^(intro¦home¦services¦advice¦webdesign¦clases¦contact¦writeEmail¦aboutme¦notfound)/ $1.php [NC]
#end begin convert dynamic urls into static urls

#begin adding an ending slash for those url ending in strings which are folders in case the endind slash is missing
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_URI} !(.*)/$[NC]
RewriteRule ^(.*)$ %{REQUEST_URI}/ [L,NC] #This line adds a slash to the end of the url
#end adding an ending slash for those url ending in strings which are folders in case the endind slash is missing

#begin removing the part of the end of the url which is not needed
#Sees whether the requested URI ends in that pattern
RewriteCond %{REQUEST_URI} (intro¦home¦services¦advice¦webdesign¦clases¦contact¦writeEmail¦aboutme¦notfound)/(.+)$ [NC]
#Rewrite the URI with the URL specified
RewriteRule .* http://www.example.com/%1/ [L,R=301,NC]
#end removing the part of the end of the url which is not needed

#begin removing the not needed slash after the first slash following example.com
RewriteCond %{REQUEST_URI} /(/+)$[NC]
RewriteRule .* http://www.example.com/ [L,R=301,NC]
#begin removing the not needed slash after the first slash following example.com

#begin redirect to custom error page
ErrorDocument 404 /notfound.php
#end redirect to custom error page

[edited by: jdMorgan at 5:37 pm (utc) on Dec. 16, 2009]
[edit reason] Deleted commented-out rules to shorten the post. [/edit]

thosecars82

5:58 pm on Dec 16, 2009 (gmt 0)

10+ Year Member



It is solved. I just had to include a .htaccess folder in the wordpress blog with this content:

DirectoryIndex index.php

jdMorgan

6:10 pm on Dec 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I can't begin to digest this much code, so I'm going to suggest that you look at it very carefully and get rid of the huge number of redundant rules and RewriteCond directives.

For example, these two rules


#Begin delete old dynamic link from google index with a redirect R = 301. Si solo se pone R, se pone 302 por defecto
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^language=sp$
RewriteRule .* http://www.example.com/? [L,R=301]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^language=en$
RewriteRule .* http://www.example.com/intro/language/en/? [L,R=301]
#End delete old dynamic link from google index

can be replaced with this one

#Begin delete old dynamic link from google index with a redirect R = 301. Si solo se pone R, se pone 302 por defecto
RewriteCond %{QUERY_STRING}>intro/language/en/ ^language=(sp>.+¦en>(.+))$
RewriteRule ^$ http://www.example.com/%2? [L,R=301]
#End delete old dynamic link from google index

Note that the URL-path pattern has also been moved to the RewriteRule, eliminating an additional one-third of the code in this rule.

As another example, these two rules


#begin convert dynamic urls into static urls
RewriteRule ^(intro¦home¦services¦advice¦webdesign¦clases¦contact¦writeEmail¦aboutme¦notfound)/language/([^/]*) $1.php?language=$2 [L,NC]
RewriteRule ^(intro¦home¦services¦advice¦webdesign¦clases¦contact¦writeEmail¦aboutme¦notfound)/language/([^/]+)/ $1.php?language=$2 [L,NC]

can be replaced with this one

#begin convert dynamic urls into static urls
RewriteRule ^(intro¦home¦services¦advice¦webdesign¦clases¦contact¦writeEmail¦aboutme¦notfound)/language/([^/]*[b])/?[/b]$ $1.php?language=$2 [L,NC]

Simply by making that trailing slash optional by following it with a "?"

And in fact, if the intent was *not* to require that the requested URL end with that slash or at the character preceding that slash then you only need your first rule, since that pattern is not end-anchored and therefore would accept *anything* following the URL-path specified by the pattern.

You've also got internal rewrite [L] rules preceding external redirect [R=301,L] rules, which is almost certain to cause a disaster.

---

It's also not clear which rule you added that 'broke' your WordPress, or what the URL-path to your WordPress pages and files might be. We need to know these things.

If you can narrow-down which rule it is that causes your WP to fail, we can help fix that rule. However, once that is done, this code needs to be re-written. I suspect you could reduce its execution time to less than one quarter of what it requires now... This is meant as earnest advice, and not as criticism.

Jim

thosecars82

2:25 pm on Dec 31, 2009 (gmt 0)

10+ Year Member



Thanks jdMorgan
What does this syntax mean?

RewriteCond %{QUERY_STRING}>intro/language/en/ ^language=(sp>.+¦en>(.+))$
RewriteRule ^$ http://www.example.com/%2? [L,R=301]

I do not get it. Moreover, when I just tested, it does not work the same as the other lines. So, should I guess there might be something wrong with these two lines?

I would just put
RewriteCond %{QUERY_STRING} ^language=(sp¦en)$
RewriteRule ^$ http://www.example.com/intro/language/%1? [L,R=301]
Is that what you meant?
Thanks

jdMorgan

10:02 pm on Dec 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteCond %{QUERY_STRING}>intro/language/en/ ^language=(sp>.+¦en>(.+))$
RewriteRule ^$ http://www.example.com/%2? [L,R=301]

It means 'If the query string plus the fixed text string ">intro/language/en/" matches either "language=sp>{anything}" or "language=en>intro/language/en/" then redirect and clear the query string. If the language was Spanish, redirect to "/" and if the language was English, then redirect to "/intro/language/en/".'

It's just a short way of combining your two rules. Note that "%2" will be empty for Spanish and contain the "/intro..." path for English.

The ">" implies concatenation, but in fact has no special meaning and only serves to demarcate the two 'pieces' of the input string so that the pattern can tell where the first ends and the second begins.

If it didn't work, then make sure that you replaced the broken pipe "¦" character with a solid pipe; Posting on this forum modifies the pipe characters.

Jim

thosecars82

11:29 am on Jan 1, 2010 (gmt 0)

10+ Year Member



I was looking into
[httpd.apache.org...]
but I saw nothing about using the ">" for appending purposes. Where could I find that information?
Thanks

jdMorgan

7:03 pm on Jan 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As posted above:

The ">" implies concatenation, but in fact has no special meaning...

I use it as a 'visual cue' and as stated, to demarcate the 'boundary' between the concatentated values so that the regular-expressions pattern can 'exactly match' *both* pieces, and won't be 'fooled' if one variable-value or fixed string-part contains part of the expected value for the other variable or string.

This isn't really a mod_rewrite subject, it's a regular-expressions topic. The only fact you need to take from the mod_rewrite documentation is that you can put either variables or fixed strings on the 'left side' of a RewriteCond, and you can put multiple variables and strings there, and then test the result of the concatenation of these variable-values and fixed strings using a regular-expressions pattern on the right side of the RewriteCond.

It's useful for building 'lookup tables' and for rolling-up multiple rules into single rules as in the example here.

Jim