Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

After having read a lot of posts on the topic of URL rewriting, I still remain stuck with 404 status errors.

My goal:

A 'static' rewrite of URLs, as follows (user clicks or enters the one URL/script, but internally the other one is being executed - the parameters are also 'static', hence I think I cannot use any kind of pattern (*.) and $1 in the rule...:

  • domain.tld/en/abc.php?foo to domain.tld/en/xyz.php?bar (1st level)
  • domain.tld/en/abc.php?foo&blabla to domain.tld/en/xyz.php?bar&otherstring (2nd level)

Additional Info:

  • Directory 'en' exists.
  • Script abc.php does not exist
  • Script xyz.php exists and works fine with the given static parameter(s)

My .htaccess:

Options +FollowSymLinks 
RewriteEngine On
RewriteRule ^en/abc\.php\?foo&blabla$ /en/xyz.php?bar&otherstring [NC,L]
RewriteRule ^en/abc\.php\?foo$ /en/xyz.php?bar [NC,L]

I already get a 404 error on this simpler last rule. I don't have access to the rewritelog.

share|improve this question
    
Please do not cross-post questions to other Stack Exchange sites: stackoverflow.com/questions/25114786/… –  dan Aug 5 at 7:36
    
Sorry. I only realised later that this webmasters' site may be better suited. –  LarryG001 Aug 5 at 8:51
    
NBD, it is better suited here because it's not about programming and will likely get closed there anyway. That's just to let others know to check the answers there too (and a reminder for them). Welcome to the site by the way. –  dan Aug 5 at 8:59

1 Answer 1

up vote 2 down vote accepted

You can't check for the query string using the RewriteRule pattern (the query string is stripped before pattern matching, as is the hostname, etc). So, the rules you stated above simply won't match and you'll get a 404. However, you can use the RewriteCond directive to check the query string:

For example:

domain.tld/en/abc.php?foo to domain.tld/en/xyz.php?bar

RewriteCond %{QUERY_STRING} =foo
RewriteRule ^en/abc\.php$ /en/xyz.php?bar [L]

Note that this matches the exact query string "foo". I've removed the NC flag, unless you specifically need a case-insensitive match?

share|improve this answer
    
OK,thx. So am I right when for 2 params, it would be: RewriteCond %{HTTP_QUERY_STRING}=foo&blabla Rewriterule ^en/abc\.php$ /en/xyz.php?bar&otherstring [L] –  LarryG001 Aug 5 at 8:54
    
Yes, that looks OK. Note there is a space before the =. (The = operator makes an exact string match as opposed to a regular expression.) –  w3d Aug 5 at 8:57
    
I just tested your solution, but adding the RewriteCond to my .htaccess produced a 500 Internal Server Error. When removing the RewriteCond, the 500 error is gone. ?? –  LarryG001 Aug 5 at 9:02
    
Sorry, it should be QUERY_STRING, not HTTP_QUERY_STRING as in my original post, just updated it. –  w3d Aug 5 at 9:03
1  
Yes !!!!! Everything works fine according to your solution!!! And you saved my day (and night) ! –  LarryG001 Aug 5 at 9:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.