301 redirect a query string to the same URL

Hi guys,

I’m new here and I really hope you could help me to solve my problem.

I’ve got a WP site and this query string:
?submit=view

has been added to all my URLs from my drop-down menu on the site bar. I’d like to ask how to redirect for example this:

http://mysite.co.uk/something/?submit=view (which is the URL generated from the drop down menu)

to: http://mysite.co.uk/something/ (huge duplicate content issue as you know)

Everything I’ve tried so far has redirected my internal pages to the home page and I simply can not figure it out.

I’ll be really glad and thankful if somebody helps me here :slight_smile:

Viyach,

First, WELCOME to SitePoint’s Apache forum!

Now, it appears that your “specification” is to remove the query string when it’s simply submit=view. That would require that you attempt to match EXACTLY submit=view in the query string then redirect and remove the query string, i.e.,

RewriteEngine on
RewriteCond %{QUERY_STRING} ^submit=view$
RewriteRule .? %{REQUEST_URI}? [R=301,L]

Here, I’ve ensured that the RewriteEngine is NOT in comment mode, attempted to match the specified query string the, if it does, simply remove the query string from the {REQUEST_URI} string with the trailing ?. The R=301 makes this visible to the browser and the Last flag effects the redirection.

Regards,

DK

Thank you so much for your reply dklynn. I used your code, but unfortunately it redirects all pages to the home page. :injured:

Viyach,

There is something else redirecting. Please show your entire .htaccess.

Regards,

DK

May be it’s the 404.php which is:

<?php header(“HTTP/1.1 301 Moved Permanently”);
header("Location: ".get_bloginfo(‘url’));
exit();
?>

My .htaccess is:

Options +FollowSymLinks
DirectoryIndex index.php

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} [1]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://mysite.co.uk/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

BEGIN WordPress

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

END WordPress


  1. A-Z ↩︎

Viyach,

With your WP code, you’ll never see the 404.php script.

Options +FollowSymLinks
DirectoryIndex index.php

RewriteEngine On
[indent]Fine - ensures you're NOT in comment mode.[/indent]]
RewriteBase /
[indent]NOT fine - where's the Redirect this is supposed to UNDO?[/indent]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\\ /index\\.php\\ HTTP/
RewriteRule ^index\\.php$ http://mysite.co.uk/ [R=301,L]
[indent]Removes index.php from the browser's location box.[/indent]
 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
[indent]Sends EVERYTHING (which is not a file or directory) to index.php. 
What happens to the empty {REQUEST_URI}?  
Well, it's already handled by the DirectoryIndex 
but I prefer to use .? in the regex for 
unconditional redirections like this.[/indent]

# BEGIN WordPress
[COLOR="Red"]<IfModule mod_rewrite.c>[/COLOR]
RewriteEngine On
RewriteBase /
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
[COLOR="Red"]</IfModule>[/COLOR]

# END WordPress

[indent][standard rant #4][indent]The definition of an idiot is someone who repeatedly 
does the same thing expecting a different result.  
Asking Apache to confirm the existence of ANY 
module with an <IfModule> ... </IfModule> wrapper 
is the same thing in the webmaster world.  DON'T BE 
AN IDIOT!  If you don't know whether a module is 
enabled, run the test ONCE then REMOVE the wrapper 
as it is EXTREMELY wasteful of Apache's resources 
(and should NEVER be allowed on a shared server).[/indent][/standard rant #4][/indent]
[indent]WHY repeat the RewriteEngine on?  
WHY repeat the useless RewriteBase /? 
WHY repeat ALL this?[/indent]

Then, WHERE is the query string killer code:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^submit=view$
RewriteRule .? %{REQUEST_URI}? [R=301,L]

No wonder mod_rewrite won’t kill the submit=view. If you merely want to kill all query strings, add an ? without a query string to one of the many index.php redirections, preferably the first one which is unconditional (except for file and directory checks).

Regards,

DK

Thanks a lot dklynn,

I have to admit I’m completely lost: It is obvious that I don’t know much about the subject and from your comment I understand that I’ll have to remove this part:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

About:

BEGIN WordPress

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

END WordPress

This is the original WP .htaccess and have decided to keep it and yes, the RewriteBase / repeats, but I need the part which removes the index.php:

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} [1]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://mobiledealstop.co.uk/ [R=301,L]

and I don’t know how to avoid this repetition by keeping both parts. Do I just delete RewriteEngine On
RewriteBase / ?

I will ask you (if not too much) to give me the complete .htaccess which I need. Except the necessary WordPress functions I need just to remove the index.php and all extensions such as
?submit=view


  1. A-Z ↩︎

What dklynn is saying is that you need to add the code from post #2 in this thread and modify the .htaccess as per post #6 in this thread, as there is quite a lot of unnecessary stuff in there. So, remove the lines in red, and add the lines in blue:


Options +FollowSymLinks
DirectoryIndex index.php

RewriteEngine On
[COLOR="Red"]RewriteBase / # You don't have any Alias directives, so you don't need RewriteBase[/COLOR]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\\ /index\\.php\\ HTTP/
RewriteRule ^index\\.php$ http://mysite.co.uk/ [R=301,L]

[COLOR="Blue"]# Removes submit=view from the query string
RewriteCond %{QUERY_STRING} ^submit=view$
RewriteRule .? %{REQUEST_URI}? [R=301,L]
[/COLOR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# BEGIN WordPress
[COLOR="Red"]<IfModule mod_rewrite.c> # no need to check if mod_rewrite is enable for [I]every single reuqest[/I][/COLOR]
[COLOR="Red"]RewriteEngine On # you already have that above -- remove this line[/COLOR]
[COLOR="Red"]RewriteBase / # You don't have any Alias directives, so you don't need RewriteBase[/COLOR]
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
[COLOR="Red"]</IfModule>[/COLOR]

# END WordPress

Does that help?

Yes, thank you very much :wink:

I’ve gust one quick question:

will this code be O.K. to 301 redirect the mysite.com/?page_id=xxxx URLs to something like: mysite.com/nice-title/:

RewriteCond %{QUERY_STRING} !^page_id=$
RewriteRule .* http://mysite.com%{REQUEST_URI}? [R=301,L]

my robots.tx disables these URLs but Google can still find them and I’d like to make sure this does not happen and all my pages have been properly redirected.

No, the code you provided is LOOPY (will redirect forever and never stop).
Is xxxx the nice-title you want, or is it just a number? If it’s a number there is nothing much you can do with RewriteRules. You’d better refer to the Redirect directive in that case and type out all redirections manually.

the xxxx is a number or the page ID, which is unique for every page. I hoped to redirect these URLs to the URLs with the nice permalink, for example:

mysite.com/?page_id=1234 to: mysite.com/apples/
mysite.com/?page_id=1235 to: mysite.com/bananas/

I’ve tried some WP permalink redirect plugins, but still without success :frowning: I guess the only way is to disallow the /?page_id=* and that’s it?! To redirect every single page is a lot of work - I’ve got 1500 pages…

Since Apache has no way of knowing that 1234 should be rewritten to “apples” you can’t write a RewriteRule for this.
You could take a look at RewriteMap if you have server config / virtual host access. You can’t use it in .htaccess files.
Using RewriteMap you can create a script that takes an URL as input and should return a new URL as output. In this script you can access the database to ask what the URL for id 1234 should be, and then return that.
I’m sure you can find a few tutorials on this :slight_smile:

Thanks ScallioXTX,

I guess WordPress doesn’t make my life any easier

VI,

IMHO, you need to “think out of the box” and change from using record IDs for your links to using the record’s title field (with some conversion, of course). I believe WP has implemented that for their code and I’d implemented it for http://wilderness-wally.com/ and it works a treat (if you can keep some characters OUT of the title - like ? and #, for instance).

Anyway, back to your question about the empty page_id value: It simply won’t show in the URI like this if it’s from a form as the default form handler will ignore empty values (not create the key for a null value). Therefore, test for an empty string, not ^page_id=$.

Regards,

DK

I’m sorry guys, but I’ll have to return once again to the first question: I’ve done everything as advised, but Google simply does not care! These pages: http://mysite.co.uk/?page_id=
http://mysite.co.uk/?page_id=&submit=view and
http://mysite.co.uk/?cat=
are still crawled :frowning:
I’ve got the following in my .htaccess:

RewriteCond %{QUERY_STRING} ^submit=view$ [NC]
RewriteRule .? %{REQUEST_URI}? [R=301,L]
RewriteCond %{QUERY_STRING} ^cat=$ [NC]
RewriteRule ^(.*)$ http://mysite.co.uk/? [R=301,L]
RewriteCond %{QUERY_STRING} ^page_id=$ [NC]
RewriteRule .? http://mysite.co.uk/? [R=301,L]

Or the codes are correct and I have to wait a little bit longer?

To see if the codes are correct you’d just need to visit the old URLs yourself and see if you are redirected to the new URLs.
Indeed, it might take a while until google picks up on it, depending on how often google visits in the first place.

Yes, i forgot to mention that the old URL redirects to the new one, no problems here. May be it’s about time indeed.
Cheers