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

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]

Related

htaccess redirect if the url does not contain a specific character

I'm moving the site to a subdomain and need certain tag strings to go to the subdomain and some to remain on the main site. Problem is both have a similar tag system.
I need this type of request
https://www.site.co.uk/tags/example-tag
to go here:
https://sub.site.co.uk/tags/example-tag
but this type of request
https://www.site.co.uk/tags/view?tags=14-some-varriable
to remain unchanged and parsed to content without redirecting.
What would be the most recommended and best solution?
I have written some code to work around other redirects but this one is causing me a headache.
Cheers
For your this mentioned example, below should work.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.+)/(.+)
RewriteRule ^ https://sub.site.co.uk/%1/%2 [R]

301 redirect with query strings

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.

htaccess redirecting from rewritten dynamic urls to new dynamic urls

i am experiencing a very unique problem and i hope someone can help!
so we have recently created a new ecommerce website and we made it live and everything was working great but when we to implement our 301's from our old pages we were getting some wierd things
so the code below actually works
Redirect 301 /directory/ http://mysite.com/index.php?cat=1
this code does not
Redirect 301 /directory/sub_directory/ http://mysite.com/index.php?cat=2
the output when i try to do this redirection is "Invalid parameters specified!" on a blank webpage and in the address bar it has this
http://mysite.com/index.php?cat=1/sub_directory/
we were thinking that maybe the problem is because our old pages were dynamic but mod_rewrite was used to create more readable urls and we have also deleted all our old files because they were interfering with our new pages rendering
any help would be greatly appreciated!
thanks
That is strange, as redirect should only match the specific url listed, where as it looks like its behaving like rewriterule and partially matching the subdirectory url against the first rule..
try putting the more specific rule above the less specific, like so:
Redirect 301 /directory/sub_directory/ http://mysite.com/index.php?cat=2
Redirect 301 /directory/ http://mysite.com/index.php?cat=1
That way the more specific rule will be hit first, and the /directory/ only rule will only match if more specific matches above fail
alternatively, you could try RewriteRules:
RewriteRule ^directory/$ http://mysite.com/index.php?cat=1 [R=301,NC,L]
RewriteRule ^directory/sub_directory/$ http://mysite.com/index.php?cat=2 [R=301,NC,L]
the ^ and $ anchors should prevent any unwanted partial matching

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.

htaccess redirects that ignore query string

I've recently inherited a webserver whose IP previously belonged to a well known band's forums.
Problem is I'm now drowning in 404 errors!
The URLs in question look like this:
http://[server_ip]/forum/ucp.php?mode=register&coppa=0
http://[server_ip]/forum/viewtopic.php?f=3&t=45330
http://[server_ip]/forum/index.php+++++++++++++++++++++++++++++++++++++++++++++++Result:+%ED%E5+%...
http://[server_ip]/forum/viewtopic.php?f=3&t=44597&start=0
In an ideal world I would like to redirect any traffic going to /forum/ucp.php, /forum/viewtopic.php or /forum/index.php elsewhere regardless of query string.
Or anything going to /forum/.* elsewhere, if that's doable.
I've tried a number of different solutions with little success, any help appreciated.
Assuming that you want to redirect all traffic to /forum/.* to http://mysite.com/somedirectory, which you can replace with the actual URL you wish to redirect to, you can add the following to the .htaccess in the root directory of your sites domain.
RewriteEngine On
RewriteBase /
#for any request starting with forum
RewriteRule ^forum/ http://mysite.com/somedirectory? [NC,L,R=302]
Be sure to keep the ? if you want to remove the query string params from the original query. If you want to make it a permanent redirect, change the 302 to a 301.
Don't really know what you have tried so far but this site will probably help you.
http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/#redirects

Resources