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

报告相同问题?