Forum Moderators: phranque

Message Too Old, No Replies

Wordpress 404 exemptions

         

Milamber

2:05 am on Mar 3, 2010 (gmt 0)

10+ Year Member



I'm currently using the code below to exempt certain folders from wp's 404 handling. The issue is that when people poke around in the backend the code gets replaced with wp's default code and a portion of the site breaks. I've tried adjusting the chmod of the .htaccess file to 644 but for whatever reason the permissions won't stick.

With that said, is there some way I can pull the three exemptions out of the wp code block and still get the same effect?

Thanks


# BEGIN WordPress

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/example/
RewriteCond %{REQUEST_URI} !^/example2/sub-folder/(\d+)/
RewriteCond %{REQUEST_URI} !^/example2/(sub1|sub2|sub3)/.+
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>


# END WordPress

Milamber

3:54 pm on Mar 3, 2010 (gmt 0)

10+ Year Member



Nevermind I resolved the issue, had my rewrite rules for the exemptions after the wp code, moving them before it removed the need for the exemptions.

jdMorgan

6:22 pm on Mar 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Want to speed up your WP code?

Replace all of the above with:

# BEGIN WordPress
#
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !/index\.php$
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|css|js|xml|png|ico|pdf|doc|flv|wmv|avi|mp3)$
RewriteCond %{REQUEST_URI} !^/(example/|example2/sub-folder/[0-9]+/|example2/(sub1|sub2|sub3)/.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
#
# END WordPress

If this is not the first rule-set in your file, then you already have a "RewriteEngine on" directive above this section. You don't need a second one, so don't waste the CPU time.

I removed the <IfModule> container, as its purpose is to make the code fail silently if mod_rewrite is not enabled. This is probably done to reduce WP's support costs, as it's not very likely you'd want to proceed on a server where mod_rewrite isn't installed...

The first new exclusion doubles the speed of this code, by preventing the later rewriteconds from re-executing and checking the disk for file-exists and directory-exists on "index.php" *after* the request has already been rewritten to "index.php." Since index.php must exist for WP to function, there's no use wasting the time needed to go check the filesystem to see if it's still there.

The second new exclusion prevents "exists checks" for filetypes which are not handled by WP itself -- images, CSS and external JavaScript files, xml documents (e.g. sitemap.xml), text documents, and media files). The filetype list should be adjusted to suit your site, listing the filetypes from most-frequently-requested to least.

The third exclusion is simply your original exclusions re-coded on a single line, which is faster in .htaccess. I changed the "\d" PCRE expression to the POSIX regex "[0-9]" expression simply for better "portability" for others who might later copy the code from this post. I removed the unneeded parentheses and trailing "+" sign.

If your site is on shared hosting and is fairly busy, you might be able to see a noticeable improvement in page-load times simply by using these tweaks.

Jim

Milamber

1:55 pm on Mar 7, 2010 (gmt 0)

10+ Year Member



Thanks Jim,

I'm always eager to optimize code.

This is the code I was trying to get to work:

RewriteRule ^example/sub-folder/([0-9]+)/*$ example/sub-folder/?id=$1 [L]
RewriteRule ^example/(sub1|sub2|sub3)/([0-9]+)/([a-zA-Z]+).*$ example/$1/?page=$2&sort=$3 [R=301,L]
RewriteRule ^example/(sub1|sub2|sub3)/1/$ example/$1/ [R=301,L]
RewriteRule ^example/(sub1|sub2|sub3)/([0-9]+).*$ example/$1/?page=$2 [L]

Its working now, but it doesn't feel as clean to me as it should. Specifically, ending the lines with .* feels wrong to me.
Unfortunately I'm not that familiar with mod_rewrite and I don't want to break the site while I tinker with it.

Any thoughts?

g1smd

6:31 pm on Mar 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



External redirects should include a protocol and domain name in the target; add both of those items to the rules that should be redirects.

Rule ordering is very important. All external redirects should be listed before any of the internal rewrites. Change the order to comply with that.

If you're not capturing the trailing .* to re-use in a backreference, you can just omit the .* from the pattern.

What are those four rules supposed to do? Please # comment each line of code, as it is not clear what you want to do. I am concerned that one of those redirects is actually supposed to be a rewrite.

For rules that implement a rewrite, there must be only one URL format that can trigger that rewrite. There must not be 'optional' components to the URL request; i.e. there must be a fixed trailing slash, or there must NOT be a trailing slash; you cannot have an 'optional' trailing slash. Failure to observe this creates Duplicate Content issues.

Please repost the code with ALL of the corrections noted here included.

Milamber

7:01 pm on Mar 7, 2010 (gmt 0)

10+ Year Member



They're all internal.


# Clean url for product page
RewriteRule ^example/sub-folder/([0-9]+)/*$ example/sub-folder/?id=$1 [L]

# Category Sorting
# Clean urls broke when sorting due to wp 404 handling
# Haven't tested with preserving the clean urls since changing the code order as per post 2
RewriteRule ^example/(sub1|sub2|sub3)/([0-9]+)/([a-zA-Z]+).*$ example/$1/?page=$2&sort=$3 [R=301,L]

# Remove page reference when on page 1 of a category
RewriteRule ^example/(sub1|sub2|sub3)/1/$ example/$1/ [R=301,L]

# Clean URL for category pages
RewriteRule ^example/(sub1|sub2|sub3)/([0-9]+).*$ example/$1/?page=$2 [L]


In regards to the trailing slash or not, they should all have trailing slashes which would make the rules:


RewriteRule ^example/sub-folder/([0-9]+)/$ example/sub-folder/?id=$1 [L]
RewriteRule ^example/(sub1|sub2|sub3)/([0-9]+)/([a-zA-Z]+)/$ example/$1/?page=$2&sort=$3 [R=301,L]
RewriteRule ^example/(sub1|sub2|sub3)/1/$ example/$1/ [R=301,L]
RewriteRule ^example/(sub1|sub2|sub3)/([0-9]+)/$ example/$1/?page=$2 [L]

g1smd

7:15 pm on Mar 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Items that include R=301 *are* external redirects by definition.

External redirects should include a protocol and domain name in the target; add both of those items to the rules that should be redirects.

Rule ordering is very important. All external redirects should be listed before any of the internal rewrites. Change the order to comply with that.

All of the lines currently coded as 'rewrites' have issues that promote Duplicate Content problems.

I am concerned that one of those redirects is actually supposed to be a rewrite. If that is the case, using the [R=301] is an error.

Milamber

7:47 pm on Mar 7, 2010 (gmt 0)

10+ Year Member




# Space between http:// and domain doesn't appear in file
RewriteRule ^example/(cat1|cat2|cat2)/([0-9]+)/([a-zA-Z]+)/$ http:// original-domain.com/example/$1/?page=$2&sort=$3 [R=301,L]
RewriteRule ^example/(cat1|cat2|cat2)/1/$ http:// original-domain.com/example/$1/ [R=301,L]
RewriteRule ^example/(cat1|cat2|cat2)/([0-9]+)/$ example/$1/?page=$2 [L]
RewriteRule ^example/product-page/([0-9]+)/$ example/product-page/?id=$1 [L]