Forum Moderators: phranque

Message Too Old, No Replies

pre-process a URL prior to wordpress - help

I need to pre-process a URL prior to it being used by WordPress.

         

the_raven

6:21 am on May 4, 2010 (gmt 0)

10+ Year Member



I am using a custom paging systen on a few wordpress pages. I need to pre-process a URL prior to it being used by WordPress.

I need:
mydomain.com/catagory/page/page-1
converted to:
mydomain.com/catagory/page/?pageNum=1

prior to wordpress getting ahold of it, or some how get wordpress to to the conversion.

I have tried the following in my .httaccess without success. Any help with this is greatly appreciated.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)page-([0-9]+)$ $1?pageNum=$2 [PT]
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

g1smd

6:33 am on May 4, 2010 (gmt 0)

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



What URL do you link to from the pages of the site? The URL of the page is defined by what is in the HREF of that link.

The rules in .htaccess do not 'convert' URLs. They act on incoming URL requests, and connect those requests to a non-default internal file location in order to serve the content.

Whenever you see a pattern 'leading' with a (.*) fragment, it is always the wrong thing to use. This is because it matches the entire URL request and then has to 'back off and retry' hundreds of times until the correct match is found. Something like
(([^/]+/)*)
might be better.

Your pattern seemingly accepts the URL request
example.com/category/page/page-1
and then requests by Proxy
example.com/category/page/?pageNum=1
, however, I am guessing the rule is in the wrong place. RewriteConds affect only the single following RewriteRule. I'd guess you inserted the new Rule in the wrong place. It needs to be before the two Conditions as those Conditions are supposed to be used by the final Rule. You use of the [PT] flag will mess up your logging.

jdMorgan

3:01 pm on May 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would suggest a more-specific definition of exactly what can appear in the initial "/category/page/" part of the requested URL-path. This will result in less chance of unexpected rewrites.

Is it always "two directory levels"? Always alphanumeric, always alphabetic, always all-lowercase? Or is it always a short list of fixed names like "category, product, details"? The more specific you caa be without introducing a new on-going maintenance chore, the better.

Also, the destination of this new rule should be the WP script, not another "search-friendly" URL-path which will need to be rewritten again in order to serve content. You do not want to process the request through more than one rule, because this may trigger a well-known bug in mod_rewrite which re-injects duplicate path-parts into the substitution, and causes malfuncti0on.

The <IfModule> container is unnecessary. Its only purpose is to cause this code to fail silently on server where mod_rewrite is not installed. It is doubtful that this is what you'd really want.

Several major performance improvements can be made to the WP code. We can wait to address those issues until you get your basic function working, though.

The flag value of [PT] is "Pass-Through" which leaves the substitution in the form of a URL-path instead of converting it to a filepath. This is not to be confused with [P], which is a (reverse) proxy through-put.

Jim

the_raven

8:39 pm on May 4, 2010 (gmt 0)

10+ Year Member



Could someone please show an example of what it is I need to do to convert any url that ends with /page-([0-9]+) to now end with /?page=$1 and then pass the modified URL onto wordpress for it to process. I have also tried the following but only get 500 and 404 redirect errors.

# BEGIN WordPress
RewriteEngine On
RewriteRule ^(.*)page-([0-9]+)$ $1?pageNum=$2 [C]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

I am a novice at this incase you have not guessed, but realy need to find an answer for this problem. If Mod_rewrite is not the best solution for this and there is a way I can get wordpress to dothis internally a link to an explanation of how to do it would be appreciated.

jdMorgan

9:35 pm on May 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you care to answer the questions in the posts above, so that we can assist you?

Jim

the_raven

3:16 am on May 5, 2010 (gmt 0)

10+ Year Member



Is it always "two directory levels"? No, sometimes it is just one
mydomain.com/category-name/page-name/sub-page-#
mydomain.com/page-name/sub-page-#

# is the number of the sub-page if the sub-page = 1 the url will not have a sub-page-# and will end with page-name/
Category and page names are always lowercase alphabetic with hyphens.

I currently have one page with no category that will have sub-pages.
I have 1 category that has 5 main pages that will have sub-pages.
I have 1 category that has 2 main pages that will have sub-pages.

"Also, the destination of this new rule should be the WP script"
The /index.php is the wordpress script as far as I know.
This is the part I really can't figure out, Wordpress seems to use an internal fuction to convert the URL into a postID and any attempts I have made to mod_rewrite the url end up with the 500 or 404 errors.

I hope this answers your questions

jdMorgan

4:34 am on May 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, If WP is examining the client=requested URL, then it won't pay any attention to the rewritten URL.

The only thing that's actually wrong about the code you posted is the [C] (chain) flag. That disables the main WP rewrite unless your new page- rewriterule is invoked first.

Instead of trying to get this somewhat-complex pattern-matching rule working, you might want to try testing a dirt-simple single-URL rewrite -- Rewriting one specific category, page, and sub-page URL.

If that doesn't work, then I think you might want to try reverse-proxying this request from your server to itself using the [P] flag and a full URL. Finally, if that doesn't work (maybe mod_proxy isn't available), then a 302 redirect may be your only option... :(

These latter two options are based on forcing a new HTTP request, which effectively 're-loads' the client-requested URL variable that WP is examining.

It's also possible there's a plug-in available to do what you want -- Some code that actually modifies WP's internal behaviour so that it can do what you want (pagination, I assume).

Jim

the_raven

5:09 am on May 7, 2010 (gmt 0)

10+ Year Member



Thanks for your replies. Looks like I'll have to do this inside of wordpress, and am looking into how to do that now. with the add_rewrite() function.

jdMorgan

1:37 pm on May 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See also the thread in our Apache Forum Library about speeding up the WordPress rewrite rule [webmasterworld.com]. As currently provided by WP, it's *very* inefficient.

Jim