jacko310592 Posted March 26, 2010 Share Posted March 26, 2010 hey guys, is the following rewrite rule correct for allowing "mysite.com/user/USERNAME" from the full address of "mysite.com/user/index.php?USERNAME #Rewrite URL for User Profile, from /user/index.php?USERNAME to /user/USERNAME Options +FollowSymLinks RewriteEngine On RewriteBase / RewriteRule ^user/([a-zA-Z0-9_]+)?$ user/index.php?$1 [NC,L] ...it seems to be working fine, but im no good with expressions or rewriting in general so i thought id make sure its 100% fine and with no extra bits that arent needed. oh, and the 'USERNAME' can only be letters (A-Z, a-z), numbers (0-9) and underscores. thanks Quote Link to comment https://forums.phpfreaks.com/topic/196623-is-this-code-correct/ Share on other sites More sharing options...
cags Posted March 26, 2010 Share Posted March 26, 2010 I'm not entirely sure on the use of the question mark at the end of the matching pattern, but other than that seems fine. Quote Link to comment https://forums.phpfreaks.com/topic/196623-is-this-code-correct/#findComment-1032345 Share on other sites More sharing options...
jacko310592 Posted March 26, 2010 Author Share Posted March 26, 2010 so should it be like this... "RewriteRule ^user/([a-zA-Z0-9_]+)$ user/index.php?$1 [NC,L]" ? thanks cags Quote Link to comment https://forums.phpfreaks.com/topic/196623-is-this-code-correct/#findComment-1032348 Share on other sites More sharing options...
jacko310592 Posted March 28, 2010 Author Share Posted March 28, 2010 ive decided that i actually need this rewrite code to accept any characters so i can filter incorrect usernames on the actual page, instead of it throwing a 404 error once a username with the wrong characters is entered. so far i have this: RewriteRule ^profile/(.*)$ profile/index.php?$1 [NC,L] ...but this gives a 403 error if a url with characters such as * " % are included. can someone please tell me how to change this code to allow any characters to be entered into the username part of the url thanks Quote Link to comment https://forums.phpfreaks.com/topic/196623-is-this-code-correct/#findComment-1032987 Share on other sites More sharing options...
cags Posted March 28, 2010 Share Posted March 28, 2010 RewriteRule ^user/(.+)/?$ user/index.php?$1 [NC,L] Quote Link to comment https://forums.phpfreaks.com/topic/196623-is-this-code-correct/#findComment-1032998 Share on other sites More sharing options...
jacko310592 Posted March 28, 2010 Author Share Posted March 28, 2010 thanks cags, but this still gives me 403 and 400 errors (depending on what symbols i try using in the url), is there somthing else im missing to allow all characters? thanks Quote Link to comment https://forums.phpfreaks.com/topic/196623-is-this-code-correct/#findComment-1033000 Share on other sites More sharing options...
cags Posted March 28, 2010 Share Posted March 28, 2010 What characters are you trying to use? Many characters aren't valid URL characters. Quote Link to comment https://forums.phpfreaks.com/topic/196623-is-this-code-correct/#findComment-1033002 Share on other sites More sharing options...
jacko310592 Posted March 28, 2010 Author Share Posted March 28, 2010 i tried testing it with %, " and * Quote Link to comment https://forums.phpfreaks.com/topic/196623-is-this-code-correct/#findComment-1033003 Share on other sites More sharing options...
jacko310592 Posted March 28, 2010 Author Share Posted March 28, 2010 okay, i have another problem now =/ this is the folder structure of /user/.... /user/ /user/index.php <--- this is the index page within the user folder i am wanting to be rewritten /user/USERNAME1/ <--- each user gets a folder, this folder contains images etc now it seems with the rewrite rule that was suggest above, the images arent being found. can this problem be solved? thanks Quote Link to comment https://forums.phpfreaks.com/topic/196623-is-this-code-correct/#findComment-1033071 Share on other sites More sharing options...
cags Posted March 28, 2010 Share Posted March 28, 2010 You need to use fully qualified static URLs for the images/styles. Quote Link to comment https://forums.phpfreaks.com/topic/196623-is-this-code-correct/#findComment-1033073 Share on other sites More sharing options...
jacko310592 Posted March 28, 2010 Author Share Posted March 28, 2010 ive been using absolute URLs, as im running the site locally at the moment, the URLs ive been using are like as follows: "D:/xampp/htdocs/user/USERNAME/40cabc290f43631cb4a7844790b1f2f8.png" is this wrong? Quote Link to comment https://forums.phpfreaks.com/topic/196623-is-this-code-correct/#findComment-1033075 Share on other sites More sharing options...
cags Posted March 28, 2010 Share Posted March 28, 2010 So you actually have a folder on your server for every user? Quote Link to comment https://forums.phpfreaks.com/topic/196623-is-this-code-correct/#findComment-1033076 Share on other sites More sharing options...
cags Posted March 28, 2010 Share Posted March 28, 2010 Nevermind, it's because you wanted to match ALL characters, therefore 'user/USERNAME/40cabc290f43631cb4a7844790b1f2f8.png' matches and is redirected. Try this instead. RewriteRule ^user/([^/]+)/?$ user/index.php?$1 [NC,L] Quote Link to comment https://forums.phpfreaks.com/topic/196623-is-this-code-correct/#findComment-1033077 Share on other sites More sharing options...
jacko310592 Posted March 28, 2010 Author Share Posted March 28, 2010 yeah, when i user signs up, php will make a folder with their username under the folder /user/ (and to add to my previous reply, ive also tried url such as: "http://localhost/user/USERNAME/40cabc290f43631cb4a7844790b1f2f8.png") and thanks, that last code seems to have worked.... havnt got time to test it properly at the moment, my tea is ready xD thanks for all the help cags Quote Link to comment https://forums.phpfreaks.com/topic/196623-is-this-code-correct/#findComment-1033079 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.