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 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决