I want to set a redirect as below.

http://www.domain.com/course/view.php?id=2 TO http://www.domain.com/course/view.php?id=2&section=1

Can you please let me know how can this be set ?

  • Have you tried straight 301 redirect set in htaccess file? Apr 12, 2014 at 11:33
  • Please include what you have tried already... Apr 12, 2014 at 12:13
  • 1
    I tried the redirect from cPanel. From cPanel it does not accepts the ? & =.
    – MilesWeb
    Apr 12, 2014 at 12:24
  • What web server are you using? I am assuming Apache. Would I be right?
    – closetnoc
    Apr 12, 2014 at 14:17

2 Answers 2


You could do it in the PHP itself

<?php header("HTTP/1.1 301 Moved Permanently"); header("Location: http://www.domain.com/course/view.php?id=2&section=1"); ?>

You could do it with mod_rewrite

RewriteEngine On RewriteCond %{QUERY_STRING} ^id=2$ RewriteRule ^/?course/view\.php$ http://www.domain.com/course/view.php?id=2&section=1 [L,R=301]

  • Hi Brett. It worked like a charm. Cheers.
    – MilesWeb
    Apr 13, 2014 at 4:33

In .htaccess:

RewriteEngine On
RewriteCond %{QUERY_STRING} =id=2
RewriteRule ^course/view\.php /course/view.php?section=1 [R=301,QSA,L]

If the query string matches exactly id=2 then externally redirect. The QSA flag combines the original query string with the new one, so the resulting URL is actually ?section=1&id=2 - but the order should not matter.

The other niggling thought is that you would perhaps be better handling the defaulting of URL params in your server-side code and set a rel="canonical" tag instead of redirecting.


I tried the redirect from cPanel. From cPanel it does not accepts the ? & =

The redirection tool in cPanel can only handle simple redirects and to be honest I would avoid it if at all possible. When you add a redirect in cPanel it edits your .htaccess file. If you already have other directives in your .htaccess file then it has to be very clever not to break anything - and it does not appear to be that clever.

This no doubt depends on cPanel versions, but I tried to create a suitable redirect in cPanel. It didn't complain about the query string characters ? and & and allowed me to create the redirect. However, when examining the resulting directives in .htaccess it was completely wrong and would not have worked as intended!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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