doukeyong3746487 2014-09-02 04:41
浏览 58
已采纳

使用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>
  • 写回答

1条回答 默认 最新

  • dtra49684 2014-09-02 20:03
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等