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

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 ?

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

2 Answers 2

up vote 2 down vote accepted

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]

share|improve this answer
    
Hi Brett. It worked like a charm. Cheers. –  MilesWeb Apr 13 at 4:33
add comment

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.

UPDATE:

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!

share|improve this answer
    
Thank you very much for the insights. Much appreciated. –  MilesWeb Apr 13 at 4:33
add comment

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.