Forum Moderators: phranque

Message Too Old, No Replies

QUERY STRING regenerate by itself in the URL

Query String Url Rewrite Redirect

         

jmayeux

9:48 am on Jun 10, 2009 (gmt 0)

10+ Year Member



Hi, I'm updating a website on regular basis, I usually tried not to ask or post in forums before I can find a solution ; this time I'm struggling my main skills are xHTML CSS PHP and a nit of project management.

Does someone knows about this strange behavior find below ?

I have 2 new URLs for 2 pages to redirect, nothing like rocket science...
When I redirect this way :

Redirect 301 /pg/pk_de /pg/pk_de_tt

the Redirect 301 works but the URL is not clean and rewrite this in the Address bar:

http://www.example.com/pg/pk_de_tt?p=pg&id=pk_de

I would like this URL without the ?p=pg&id=pk_de that refers to nothing anymore
My htaccess already had simple rewrite rules like

DirectoryIndex index.php?p=page&id=index

Options +Indexes +MultiViews +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example.com/.*$ [NC]
RewriteRule \.(gif¦jpg¦swf¦flv¦png)$ /feed/ [R=302,L]

RewriteCond %{HTTP_HOST} !^www.example.com$
RewriteRule ^(.*) http://www.example.com/$1 [R=301]

RewriteRule ^index.html /index.php

RewriteRule ^([^/.]+)/([^/.]+)/?$ index.php?p=$1&id=$2
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ index.php?p=$1&id=$2&edito=$3
RewriteRule ^([^/.]+)/?$ index.php?p=$1

The question is How can I get a clean URL redirect without the QUERY_STRING ?

jdMorgan

1:56 pm on Jun 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The query string isn't being "regenerated," it is simply being passed through unchanged. This is the default Apache behaviour.

Remember that query strings are not part of a URL. They have no function in "locating" any resource. They are data appended to a URL to be passed to the resource at that URL. Therefore Apache directives which manipulate URLs do not even 'see' the query string, and query strings must be handled separately. For example, see the RewriteCond documentation and the %{QUERY_STRING} and %{THE_REQUEST} environment variables.

Use this rule to implement your redirect and clear the query string:


RewriteRule ^pg/pk_de$ http://www.example.com/pg/pk_de_[b]tt?[/b] [R=301,L]

The question mark at the end of the substitution URL tells mod_rewrite to replace the current query string with a blank one. However, since the new query string is blank, the question mark itself will not appear in the redirected URL. So, its function in this case is simply to remove the query string.

Do not mix "Redirect" and other mod_alias directives with mod_rewrite directives; Doing so means you cannot control the order of execution of your directives: Either mod_alias or mod_rewrite may execute first, depending on your server configuration, and that could change if you switch hosts or if your server is upgraded, breaking your code.

Add an [L] flag to all of your rules. [L] should always be used, unless you know why you do not want to use it in a specific case.

Keep all of your external redirects ahead of all of your internal rewrites, and put the rules in each of these two groups in order from most-specific patterns and conditions (fewest URLs affected) to least-specific (most URLs affected).

For example, your current third rule (which should be a redirect, not a rewrite as shown), should go before your current second rule. I would suggest that you replace your current third rule with

 RewriteRule ^index\.html$ http://www.example.com/ [R=301,L]" 

The new rule I gave above you should go ahead of your current first or second rule (in this case it doesn't matter because they are mutually-exclusive).

Escape all literal periods in your regex patterns. For example, "!^www.example.com$" should be "!^www\.example\.com$"

Jim

jmayeux

2:36 pm on Jun 10, 2009 (gmt 0)

10+ Year Member



This is just perfect answer,

I thank you, if there is anything I can do for you, let me know