dthdlv9777 2016-02-05 14:25
浏览 48
已采纳

重写规则无效或正确呈现页面

I am making a social networking site which will have many users. As such, I need a simpler, easier way to get to any users page. Assume I am logged in as Freddy, if I go to Freddy's profile page, the url will say: http://localhost/profile_page.php. If from this page, I want to go to, let's say, Alice's profile page, I can simply modify the url and type http://localhost/profile_page.php/alice rather than writing http://localhost/profile_page.php?u=alice.

I have created a .htaccess file and I have enabled rewrite_module from Apache modules in Wamp. However, the page does not load appropriately. Again, assume I am logged in as Freddy, the profile page loads perfectly, but when I edit the url to go to another users page, i.e. http://localhost/profile_page.php/Alice(who is a real user, so I expect it to go to Alice's profile page), it does not render the page as instructed via CSS and also stays on Freddy's profile page.

.htaccess:

RewriteBase /www/
RewriteEngine On

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

1条回答 默认 最新

  • dtwupu6414 2016-02-05 14:58
    关注

    Well, your rules are not considering the php file here (check that I've added the slash before all regexp in rules):

    RewriteBase /www/
    RewriteEngine On
    
    RewriteRule ^/([a-zA-Z0-9_-]+)$ profile.php?u=$1
    RewriteRule ^/([a-zA-Z0-9_-]+)/$ profile.php?u=$1
    

    With this piece of code, http://localhost/profile_page.php?u=alice will never be matched. ^([a-zA-Z0-9_-]+)/$ only matches letters, numbers and underscore, so for example the question mark cannot be matched.

    Try this (assumming that instead of profile.php you mean profile_page.php

    RewriteBase /www/
    RewriteEngine On
    
    RewriteRule ^/profile_page.php/([a-zA-Z0-9_-]+)$ profile_page.php?u=$1
    RewriteRule ^/profile_page.php/([a-zA-Z0-9_-]+)/$ profile_page.php?u=$1
    

    Another improvement. You can set both expressions in only one.

    RewriteBase /www/
    RewriteEngine On
    
    RewriteRule ^profile_page.php/([a-zA-Z0-9_-]+)/?$ profile_page.php?u=$1
    

    I highly recommend you to remove the profile_page.php here in order to increase the url readability (I see and maybe you meant that format in your specification).

    RewriteBase /www/
    RewriteEngine On
    
    RewriteRule ^/profile_page.php/([a-zA-Z0-9_-]+)/?$ profile_page.php?u=$1
    RewriteRule ^/profile/([a-zA-Z0-9_-]+)/?$ profile_page.php?u=$1
    RewriteRule ^/([a-zA-Z0-9_-]+)/?$ profile_page.php?u=$1
    

    In that case you will be able to match these urls * http://localhost.com/alice * http://localhost:com/profile/alice * http://localhost.com/profile_page.php/alice

    To be able to match your own profile, just be sure you enable as well some routes for that, for example:

    RewriteBase /www/
    RewriteEngine On
    
    RewriteRule ^/?$ profile_page.php
    RewriteRule ^/me?$ profile_page.php
    RewriteRule ^/profile_page.php/([a-zA-Z0-9_-]+)/?$ profile_page.php?u=$1
    RewriteRule ^/profile/([a-zA-Z0-9_-]+)/?$ profile_page.php?u=$1
    RewriteRule ^/([a-zA-Z0-9_-]+)/?$ profile_page.php?u=$1
    

    Check that order matters here. The first that matches, the first that is applied. In that case I've used the same endpoint file. Check that $_GET['u'] isset in order to load the specified user or the one in session.

    I'd highly recommend you to use some kind of front controller in order to be able to manage all routes given one single class (app.php for example), like Symfony or any modern PHP framework does.

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

报告相同问题?

悬赏问题

  • ¥15 14名同学选出十名到西和,礼县,武都宕昌参加实习,每个学校一个人
  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路