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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法