douyong5476 2013-02-12 21:13
浏览 25
已采纳

htaccess从不同的页面重写url

i make this in htaccess file .. for rewrite url from different page

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1 [QSA]    
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?username=$1 [QSA]   

and it's works fine ..

now i need to add more as facebook

you can put facebook.com/username

which is : facebook.com/profile.php?username=

& you can put facebook.com/pagename which is : facebook.com/pages.php?username=

and works fine

from different pages ..

I need to make like this ..

any help ?!

RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1 [QSA]    
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?username=$1 [QSA]   

RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?username=$1 [QSA]  
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?username=$1 [QSA] 
  • 写回答

1条回答 默认 最新

  • duanfu9523 2013-02-12 21:25
    关注

    Your regex patterns match identically, so you're going to need something to differentiate between pages and people (or redirection to profile.php vs. index.php).

    You can accomplish this one of two ways. First, you can go all to one page, let's call it redirect.php. On this page, you could check the id value and then redirect. So like:

        RewriteRule ^([a-zA-Z0-9_-]+)/?$ redirect.php?id=$1
    

    In redirect.php, you grab $_GET['id'] and run some query against it to determine if it's a page or person, and then redirect using something like:

        header("Location: profile.php?username=".$_GET['id']);
    

    If you strictly looking for Facebook related stuff, you can hit the graph API up instead of the database to determine if its a page or person ID. (Not sure if it can exist in both - but I think not).

    If you don't have a database or data source to determine if its a page or person, then you'll need to add something to your URL strings to determine, like /pages/{pageid}, which you could then rewrite your rules to:

        RewriteRule pages/([a-zA-Z0-9_-]+)/?$ index.php?username=$1  # for pages
        RewriteRule ^([a-zA-Z0-9_-]+)/?$ profile.php?username=$1  # for people
    

    If you can't use something in your URL and don't have a way to access it via database, then you're out of luck, as you're hitting two identical patterns.

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

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line