Adam Posted June 15, 2009 Share Posted June 15, 2009 Hi guys. Having some trouble with this mod_rewrite rule: RewriteRule ^forgot\?([0-9]+)$ request.php?forgot_code=$1 [L] I'm trying to match URLs like "/forgot?123456". The regex doesn't seem wrong, but just doesn't work. Can anybody spot why? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/162264-rewriterule-doesnt-work/ Share on other sites More sharing options...
corbin Posted June 15, 2009 Share Posted June 15, 2009 This could be entirely wrong, but if I remember correctly, the query string is not included in the URL during the RewriteRule statement (kind of strange, but makes sense if you think about the HTTP specification). You could do something like: RewriteCond %{QUERY_STRING} ^forgot\?([0-9]+)$ RewriteRule ^$ /request.php?forgot_code=%1 Quote Link to comment https://forums.phpfreaks.com/topic/162264-rewriterule-doesnt-work/#findComment-856526 Share on other sites More sharing options...
Adam Posted June 15, 2009 Author Share Posted June 15, 2009 Thanks for taking a look. Unfortunately though when I try to test that I'm just getting a 404 error, seems like it's trying to find it as an actual file. In case anyone asks as well here's the full file: RewriteEngine On RewriteCond %{QUERY_STRING} ^forgot\?([0-9]+)$ RewriteRule ^$ /request.php?forgot_code=%1 Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/162264-rewriterule-doesnt-work/#findComment-856539 Share on other sites More sharing options...
corbin Posted June 15, 2009 Share Posted June 15, 2009 Oh wow.... I'm an idiot... Not sure why I went with ^$.... ^$ matches an empty string lol. Try: RewriteEngine On RewriteCond %{QUERY_STRING} ^forgot\?([0-9]+)$ RewriteRule ^ /request.php?forgot_code=%1 Or, if you want to be more proper, you could just do something like: RewriteEngine On RewriteCond %{QUERY_STRING} ^forgot\?([0-9]+)$ RewriteRule .* /request.php?forgot_code=%1 Or perhaps even: RewriteEngine On RewriteCond %{QUERY_STRING} ^forgot\?([0-9]+)$ RewriteRule . /request.php?forgot_code=%1 Quote Link to comment https://forums.phpfreaks.com/topic/162264-rewriterule-doesnt-work/#findComment-856649 Share on other sites More sharing options...
Adam Posted June 16, 2009 Author Share Posted June 16, 2009 Tried all 3, but no luck :-\ Getting a 'not found' error for them all. You can see the result here. Thanks for your help. Do you have any other suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/162264-rewriterule-doesnt-work/#findComment-857031 Share on other sites More sharing options...
corbin Posted June 17, 2009 Share Posted June 17, 2009 Ahhh.... QUERY_STRING as the name implies, is only the query string.... (For page?blah, only 'blah' would be included.) Meaning you either need to change that to be REQUEST_URI or change the rewrite to something like this: RewriteEngine On RewriteCond %{QUERY_STRING} ^([0-9]+)$ RewriteRule ^forgot$ /request.php?forgot_code=%1 I have tested that one, and it works for me. You could just change QUERY_STRING to REQUEST_URI if you wanted, but either way should work. (The REQUEST_URI way might be more efficient if you have any other pages that will have the format page?numbers, and only numbers will be after the ?.) Quote Link to comment https://forums.phpfreaks.com/topic/162264-rewriterule-doesnt-work/#findComment-858240 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.