TecTao Posted June 28, 2012 Share Posted June 28, 2012 On a membership site, a user has a code consisting of two capital letters and and series of numbers, such as FL830. We are giving them one page web pages, website.php, that will query the DB and display user information. What I want if for a viewer to only have to type in www.examplesite.com/FL830 They would ending seeing the page as if it were a GET request www.examplesite.com/webpage.php?rep_id=FL830 I have written a mod_rewrite in the .htaccess file in the root of the site. The following code is The rule that I'm trying to write is from the friendly URL to the GET request URL. I don't know if this is possible. Here's the code I've writtne RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*).com/([A-Z]+)-([0-9]+)$ (.*).com/webpage.php?rep_id=$1-$2 [L] The (.*).com/([A-Z]+)-([0-9]+) is the website URL/rep_id code. The (.*).com/webpage.php?rep_id=$1-$2 is translating it back to the GET request with two masks, the Alpha part and the Numeric part This is not working. If I type in the URL www.marketingteammates.com/webpage.php?rep_id=FL830 the page comes up with the users information. If I type in www.marketingteammates.com/FL830 it errors with a 404 page not found. Quote Link to comment https://forums.phpfreaks.com/topic/264963-cant-get-mod_rewrite-to-work-friendly-url-to-access-web-page/ Share on other sites More sharing options...
DavidAM Posted June 28, 2012 Share Posted June 28, 2012 Your rewrite rule is looking for one or more uppercase letters followed by a dash followed by one or more digits. Your description of the problem and your example do not indicate there should be a dash there. Your rewrite rule is also supplying a dash in the GET variable; again, not mentioned in the problem statement. From the description of the problem and the example, I would use: RewriteRule ^([A-Z]{2,2}[0-9]+)$ webpage.php?rep_id=$1 [L] Also, I just noticed, you are capturing the domain name so IT would be $1, the letters would be $2 and the digits would be $3. Oh yeah, and you don't get the domain name for the rewrite rule, you only get the page (and directories) Quote Link to comment https://forums.phpfreaks.com/topic/264963-cant-get-mod_rewrite-to-work-friendly-url-to-access-web-page/#findComment-1357795 Share on other sites More sharing options...
TecTao Posted June 28, 2012 Author Share Posted June 28, 2012 Thanks, I picked up on part of that too, the new rule I've uploaded is RewriteRule ^([A-Z])([A-Z])([0-9]+)$ /webpage.php?rep_id=$1$2$3 [R,NC,L] I believe it's now looking for one capital letter next to one capital letter next to any series of numbers. I'm assuming that the variable that is passed would now be rep_id=FL830 Problem is I'm still getting a 404 page does not exist. Quote Link to comment https://forums.phpfreaks.com/topic/264963-cant-get-mod_rewrite-to-work-friendly-url-to-access-web-page/#findComment-1357815 Share on other sites More sharing options...
TecTao Posted June 28, 2012 Author Share Posted June 28, 2012 Thanks, your observation caught it and it works. So, now I have a question whether it can have a new rule that would reverse itself. When I put in www.examplesite.com/FL830 it refreshes to the page and the address bar shows www.examplesite.com/webpage.php?rep_id=FL830 I would like it to just show the www.examplesite.com/FL830 in the address bar I tried writing a new rule but that did the opposite of the first rule and it didn't work. Any thoughts on part of the rewriteRule I missed? Current rule is RewriteRule ^([A-Za-z])([A-Za-z])([0-9]+)$ /webpage.php?rep_id=$1$2$3 [R,NC,L] Thanks again for the earlier help. Quote Link to comment https://forums.phpfreaks.com/topic/264963-cant-get-mod_rewrite-to-work-friendly-url-to-access-web-page/#findComment-1357823 Share on other sites More sharing options...
DavidAM Posted June 29, 2012 Share Posted June 29, 2012 That has to do with the flags you have on the rewrite rule. The "R" tells Apache to redirect to the url that you have built. Take the "R" out and it should work as you expect. Also, there is no reason to capture those values separately, you can use a single capture group RewriteRule ^([A-Za-z][A-Za-z][0-9]+)$ /webpage.php?rep_id=$1 [NC,L] Quote Link to comment https://forums.phpfreaks.com/topic/264963-cant-get-mod_rewrite-to-work-friendly-url-to-access-web-page/#findComment-1357841 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.