Redirect for special characters
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§ion=1
Can you please let me know how can this be set ?
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§ion=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§ion=1 [L,R=301]
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!
Comments
Post a Comment