draj30958 2012-06-05 21:28
浏览 36
已采纳

条件.htaccess重定向 - 根据网址重定向到两个不同的页面

My website functions a little like 9gag or damnlol, etc. when the user visits an item page, for example: site.com/104-funny-thing, the rewrite rule redirects to view.php?id=104-funny-thing. Here the URL is stripped away of everything after the first '-'. So we are left with the id only, in this case: 104, and the query gets the item.

Here is the Rule:

RewriteRule ^([A-Za-z0-9-]+)$ view.php?id=$1

What I want is that the same URL format could be used to open up user profiles as well. For example: site.com/salman, this URL should open up profile.php?username=salman.

I thought of doing this via PHP. Where I can use the same file, view.php, for both purposes. Once the parsing of the URL is done, if I get a numeric value, I would include the item.php page and query and show data accordingly. If it's not numerical, then I ca include the profile.php page and show the profile instead. This should theoretically work flawlessly, but what I wanted to try was that if there was a way to redirect using the .htaccess directly, I would prefer to use that option, as the previous option requires extra work to sort out some issues that the solution causes.

Someone good at htaccess (regex code?) might be able to offer a solution. Here is some further explanation:

  • Usernames can contain numbers, but not only numbers
  • Username URLs cannot contain '-'
  • Item IDs cannot contain alphabets
  • URLs 104-funny, 104- and 104, all work for view.php

Thank you for your help in advanced!

  • 写回答

1条回答 默认 最新

  • dsbgltg159136540 2012-06-06 10:08
    关注
    #article urls
    RewriteRule ^([0-9]+)(-.*)?$ view.php?id=$1
    
    #profile urls.
    RewriteRule ^([A-Za-z0-9]+)$ profile.php?username=$1
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示