Remove get parameters in a redirection

New to mod_rewrite? This is a good place to start.

Remove get parameters in a redirection

Postby deeppurple » Thu Oct 15, 2009 7:40 am

Hi,

I try to do this:

from
http:/ /www.domain.com/page.php?section=first-chapter

an redirect to:
http:/ /www.domain.com/first-chapter







I was trying with this rule:

RewriteCond %{QUERY_STRING} ^section=(.*)$
RewriteRule ^page.php$ /%1 [R=301,L]

and the result was:

from
http:/ /www.domain.com/page.php?section=first-chapter

and the redirection was:
http:/ /www.domain.com/first-chapter?section=first-chapter

how can remove the "?section=first-chapter" parameter?

thanks for help.
deeppurple
 
Posts: 2
Joined: Thu Oct 15, 2009 7:26 am

Postby richardk » Thu Oct 15, 2009 2:20 pm

Adding a ? to the end of the new URL will set an "empty" query string for the new URL.
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{QUERY_STRING} ^(.*&)?section=([^&]+)(&.*)?$ [NC]
RewriteRule ^page\.php$ /%2? [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby deeppurple » Thu Oct 15, 2009 5:59 pm

thank you so much, works fine.
deeppurple
 
Posts: 2
Joined: Thu Oct 15, 2009 7:26 am

Postby alternapop » Wed Oct 21, 2009 1:16 pm

how is this done when redirecting from one url to another so that the "?ID=9300" section is removed? i have a rewrite working mostly but it still includes the ending string.

www.exampleone.com/projects/detail.php?ID=9300

redirects to:

www.exampletwo.com/team/?ID=9300

when i'd like it to redirect to:

www.exampletwo.com/team/

this is what i have:

Code: Select all
   <IfModule mod_rewrite.c>
      RewriteEngine On
      RewriteCond %{HTTP_HOST} ^(.*)deptone\.example\.edu [NC]
      RewriteCond %{HTTP_HOST} !^$
      RewriteRule ^(.*) http://depttwo.example.edu/team/ [L,R=301,NC]
   </IfModule>


thanks!
alternapop
 
Posts: 2
Joined: Wed Oct 21, 2009 1:08 pm

Postby richardk » Thu Oct 22, 2009 10:26 am

All query strings or just the ID=9300 variable?

For all query strings adding a ? to the new new URL sets an "empty" query string.
Code: Select all
   Options +FollowSymLinks
   <IfModule mod_rewrite.c>
      RewriteEngine On

      RewriteCond %{HTTP_HOST} ^(.+\.)?deptone\.example\.edu$ [NC]
      RewriteRule ^(.*)$ http://depttwo.example.edu/team/? [NC,R=301,L]
   </IfModule>


Or do you want to redirect only /projects/detail.php?ID=9300 to /team/?
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(.+\.)?deptone\.example\.edu$ [NC]
RewriteCond %{QUERY_STRING} ^(.*&)?ID=9300(&.*)?$ [NC]
RewriteRule ^projects/detail\.php$ http://depttwo.example.edu/team/? [NC,R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby alternapop » Thu Oct 22, 2009 10:37 am

for any and all text after the "?"...

thanks a lot, that's working.

cheers!
alternapop
 
Posts: 2
Joined: Wed Oct 21, 2009 1:08 pm


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 32 guests

cron