php - Rewrite Rule not working or rendering page properly -
i making social networking site have many users. such, need simpler, easier way users page. assume logged in freddy, if go freddy's profile page, url say: http://localhost/profile_page.php
. if page, want go to, let's say, alice's profile page, can modify url , type http://localhost/profile_page.php/alice
rather writing http://localhost/profile_page.php?u=alice
.
i have created .htaccess file , have enabled rewrite_module
apache modules in wamp. however, page not load appropriately. again, assume logged in freddy, profile page loads perfectly, when edit url go users page, i.e. http://localhost/profile_page.php/alice
(who real user, expect go alice's profile page), not render page instructed via css , 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, rules not considering php file here (check i've added slash before 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 piece of code, http://localhost/profile_page.php?u=alice never matched. ^([a-za-z0-9_-]+)/$
matches letters, numbers , underscore, example question mark cannot matched.
try (assumming instead of profile.php 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. can set both expressions in one.
rewritebase /www/ rewriteengine on rewriterule ^profile_page.php/([a-za-z0-9_-]+)/?$ profile_page.php?u=$1
i highly recommend remove profile_page.php here in order increase url readability (i see , maybe meant format in 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 case able match these urls * http://localhost.com/alice * http://localhost:com/profile/alice * http://localhost.com/profile_page.php/alice
to able match own profile, sure enable routes that, 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 order matters here. first matches, first applied. in case i've used same endpoint file. check $_get['u'] isset in order load specified user or 1 in session.
i'd highly recommend use kind of front controller in order able manage routes given 1 single class (app.php example), symfony or modern php framework does.
Comments
Post a Comment