Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/162264-rewriterule-doesnt-work/
Share on other sites

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

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!

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

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 ?.)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.