简体   繁体   中英

Rewrite Rule not working or rendering page properly

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, ie 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM