使用PHP处理URL的URL缩短程序,如果别名不存在,则转到wordpress

I am using the Phurl URL shortener.

Has a nice UI for entering new shortened urls. Works great.

But I now want to use the same domain to host my wordpress site. So htaccess points everything to redirect ".php" with the "alias" as a param:

RewriteRule ^([a-zA-Z0-9_-]+)$ redirect.php?alias=$1

So if I have http://example.com/tt directing to http://example.com/article-about-time-travel then "tt" will get passed to redirect.php and there will be an entry in the database for that will get turned into a 301 to the new url.

But if it's not in the database,

something like example.com/about and this is a page within my wordpress site, I want that to get passed on to wordpress.

But I don't know how to do that. I don't think it's possible to put this logic in htaccess since there can be new shortened urls and new wordpress pages and other rewrites added.

My problem is : I don't fully understand how rewrites work within wordpress so I can't figure out how to just pass the url on so that it can be rewritten by wordpress into something useful.

I tried using the header() function with no success (mostly got infinite redirects).

Any ideas???

Thanks to all.

EDIT: Here is my .htaccess

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteOptions MaxRedirects=2
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME} !-l
   RewriteRule ^([a-zA-Z0-9_-\w#]+)$ redirect.php?alias=$1
</IfModule>

What I was trying to suggest is implementing the fallthrough as simple include().

Keep your RewriteRule as is, and amend your existing rewrite.php with loading the WordPress dispatcher (index.php) script.

<?php
// --rewrite.php-- (keep yours unchanged)
if ($r = db("SELECT url FROM shortener WHERE alias=?", $_GET->text["alias"])) {
    header("Location: $r[url]");
    exit;
}

// Fall through to WordPress if nothing matched:
unset($_GET["alias"]);
include("./index.php");
?>

Ignore the code above, keep your rewrite.php as it is, and just append the last two lines. Just ensure that only the header()/http_redirect() call is masked by exit, and your appended logic still gets run.

That'll invoke WordPress with the current request (without internel/external redirect). WP is likely to use REQUEST_URI to determine which of its static pages was requested.