301 redirect with query strings - .htaccess

I'm new to programming and brand new to this site (although it has helped out many times as I'm trying to learn this stuff...so THANKS for all the help so far!).
My question relates to 301 redirects. I've been searching this site as well as many other pages through google and can't seem to find a solution that works for me (I'm guessing that the solution is probably already out there since it seems like a common problem...but I haven't yet been able to find it).
So here it is:
I have a site where: http://homework-heroes.com/php/views/newAssignment.php?[then any query string] always goes to the same page.
For the sake of eliminating duplicate content as seen by google, I want these to always redirect to: http://homework-heroes.com/php/views/newAssignment.php?final
What is the htaccess code I should insert to accomplish this?
Thanks in advance!

Place the following in your /.htaccess file:
RewriteRngine On
RewriteRule %{QUERY_STRING} !^$
RewriteRule %{QUERY_STRING} !^final$
RewriteRule ^(php/views/newAssignment.php)$ /$1?final [R=302,NC,L]
The above basically says that if the query string is not blank and a request is being made to /php/views/newAssignment.php, then redirect to the same page with ?final as the new query string.
Alternatively, if you would like to remove the query string altogether, just remove final,leave the question mark in the rule, and remove the second condition.
If you're happy and want to make the redirect permanent, change 302 to 301.

Related

How to redirect a "bad" url that has a query string but does not start with a ? symbol?

There's a site that has had a bunch of bad links indexed and I've been asked to deal with it. There's one type of link that is giving me a headache:
http://www.example.com/category-display.html&Category_Code=some_cat_code
I tried redirecting to the home page:
Redirect 301 /category-display.html& /
That doesn't work because it adds everything past the & to the url.
In the best of worlds, I'd like to redirect to:
/app/mm.mvc?Category_Code=some_cate_code
So I tried using querystring and RewriteRule/RewriteCond but there's no query string without the ? that I can figure out, so I'm kind of stuck here.
Any ideas?
You can use this rule as your top rule in site root .htaccess:
RewriteEngine On
RewriteRule ^category-display\.html&(.*)$ /app/mm.mvc?$1 [L,NC,NE,R=301]

Having issue with query string htaccess redirect

Tried looking via search but I couldn't find a match for this particular issue.
Needing to redirect /games/ps3/?page=2 to /ps3/games/2/. All of the approaches I've tried so far won't remove the query string and grab the page value to pass into my new URL.
A little bit new to these types of redirects as I don't work with them often, so I'm guessing it might be a RedirectRule-type approach but I'm not sure.
(Note that due to how the URLs work with other pages on the site I'm having to create the rule for each platform, i.e. I need have a separate rule for both ps3 and xbox-360. So the only variable here is the page number.)
I was thinking it might work something like
RewriteRule ^games/ps3/?page=(.*)$ /ps3/games/$1/? [L,R=301]
But I think the first ? is causing the rule to fail since the second part uses it. I tried looking online to see how to resolve that possible issue but I couldn't find anything.
Ended up messing around with the rules and got this to solve the issue:
RewriteCond %{QUERY_STRING} ^page=(.+)$ [NC]
RewriteRule ^games/ps3/$ /ps3/games/%1/? [L,R=301]

redirect old wordpress ?page_id= to non-wordpress site

I used to have a WP site that I converted to a standard html site. Problem is I found doing a google search that instead of http://www.genealogyinc.com it was returning http://www.genealogyinc.com/?page_id=21, I dont know how many pages are like this but am trying to find a htaccess workaround, all the ones I found online give me 500 server errors.
Need a rewrite for any ?page_id= cause I dont know how many other numbers are out there.
Thanks
Off the top of my head, without testing, it would be something like this.
The first line looks for the page_id querystring parameter, and if it meets it, it should pass on to the second line. The rewrite rule I have below may need some tweaking, but I hope this helps you.
RewriteCond %{QUERY_STRING} page_id=(.*)$
RewriteRule $ /? [R=301,L]

301 Redirect to change structure

I have been researching redirects for a few days now and am still struggling, so I decided to post my first question here. For some reason, it is just not clicking for me.
I have redesigned and developed a client's WordPress site and need to update it's structure.
The site's current structure is:
www.domain.com/blog/postname/2011/12/26/
The new structure should be:
www.domain.com/blog/postname
I really thought this was going to be easy since all I am looking to do is drop the date, but have not been able to grasp the whole wildcard aspect and how to end what I am trying to match. Any help would be greatly appreciated. A simple answer is great, but an explanation would be even better.
I am assuming you already know how to change your WordPress permalink structure to drop the date.
To 301 redirect all of the old URLs to the new ones, add the following rules to your .htaccess file in the root of your websites domain, ahead of any existing rules that are there.
#if these 2 lines already exist, skip them and add the rest
RewriteEngine on
RewriteBase /
# if there is a request of the form /blog/post-name/yyyy/mm/dd/
RewriteCond %{REQUEST_URI} ^(/blog/[^/]+/)[0-9]{4}/[0-9]{2}/[0-9]{2}/$ [NC]
#redirect the request to the URL without the date
RewriteRule . %1 [L,R=301]
If you want to learn more about .htaccess/rewriting you can take a look at the following urls: Indepth htaccess, Brief Introduction to Rewriting, Apache Mod_rewrite.
Let me know if this works for you and/or you have any issues.

How to create a rewriterule with multiple query strings?

I have a few URL's from an old forum I wish to redirect to the same content on another forum. Here is a (fictional) example of an old URL: http://www.oldforum.com/forum/index.php?topic=21.0;msg=100
It should redirect to: http://www.newforum.com/threads/topic.11
Old and new URL's do not have anything in common, so I will be writing manual rewrites for each. I tried a number of things, but I can't get it to work. Here's what I have:
RewriteCond %{QUERY_STRING} ^topic=21.0;msg=100$
RewriteRule ^oldforum/index\.php$ http://www.newforum.com/threads/topic.11? [R=301,L]
I'm close, right?
Correct me if I'm wrong:
The user in his/her browser types:
http://www.oldforum.com/forum/index.php?topic=21.0;msg=100
and the URL, on the server side, is redirected to:
http://www.newforum.com/threads/topic.14
You have to look into your query string to be able to do a comparison.
You've got the only solution that comes in my mind here:
Using rewritemap for query string
From there you'll be able to do your own "correspondance table" or sort of.
Hope this helps.

Resources