301 redirect urls with specific string for .htaccess - string

I'm new to this coding. I've searched all over for ways to do this but I have not come up with a working solution. I'm not even sure if I am looking for the correct thing. I've been looking at query_string, but am stumped.
Basically, I would like to 301 redirect a URL such as http://domain.com/page12345.html to http://domain.com/page23456.html Where the "page12345" may read any digits and any number of digits. All pages in that format would redirect to a set page.
Thanks for your help!

This rule should redirect any request that matches /page*.html where * is any combination of numeric characters
RewriteEngine On
RewriteRule ^/page([0-9]+)\.html http://domain.com/page23456.html [R=301,L]

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]

htaccess rewrite removing consecutive numbers in filenames

I have old urls which contain consecutive numbers I like to redirect via htaccess, for example:
(im not allowed to post links yet)
www.example.com/just/another/path2/name-789-e-11-2.html
www.example.com/its/another/path3/name-789-e-11-5.html
On the new system the appended numbers dont exist anymore:
www.example.com/just/another/path2/name-789-e-11.html
www.example.com/its/another/path3/name-789-e-11.html
So the filename in the different paths is now the same, without the appended consecutive numbers.
Does anybody have a solution for that? I tried different but none of them worked.
Any help would be greatly appreciated.
You can use this redirect rule as first rule in your site root .htaccess:
RedirectMatch 301 ^/(.+\d+)-\d+\.html$ /$1.html

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

Redirecting an already redirected page

RewriteRule ^teamstore/(.*)/$ /teamproduct.php?teamproduct=$1&products=true [NC,L]
RewriteRule ^teamstore-(.*)/$ /teamproduct.php?teamproduct=$1&products=true [NC,L]
Here's the situation. We've already had theese urls rewritten, but we want to change the formatting of these pages so they're separated by hyphen instead of a slash.
I tried redirectmatch but this added additional php value parameters to the end of the urls. It came out to be
RedirectMatch 301 /teamstore/(.*) http://www.domainname.com/teamstore-$1/
Here was the result...
teamstore-valuehere//?teamproduct=2352323&productes=true
I want someone who types in the original address teamstore/info/ to get directed to teamstore-info/ - Any ideas on how to accomplish this?
The main reason is to avoid duplicate content issues with existing links in Google Search Results.
Order of the rules can matter, but if you want to 301 redirect the initial request for SEO reasons, then I would change your RewiteRules to the following.
RewriteRule ^teamstore/(.*)/$ teamstore-$1/ [R=301,L]
RewriteRule ^teamstore-(.*)/$ /teamproduct.php?teamproduct=$1&products=true [NC,L]
This will first translate teamstore/info/ to teamstore-info/ and send the appropriate 301 response. Upon second pass it will redirect to the php you want.
Note, this is not ideal as far as performance. Yet, it does accomplish the goal of making Google happy.

Resources