htaccess rewrite removing consecutive numbers in filenames - .htaccess

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

Related

.htaccess rewrite for orphaned URLs containing underscores and arguments

I only modify the .htaccess with great care for the purposes of my online store.
Some time ago, I did a website migration from osCommerce to OpenCart. This resulted in orphaned osCommerce-style URLs with these two example formats:
http://www.londonpower.com/catalog/product_info.php?products_id=75
http://www.londonpower.com/catalog/product_info.php?cPath=15&products_id=75
Lots of websites in internet-land have links to my old-style URLs, and I have about 100 of them, so I would like to redirect them to new URLs with the following format:
http://www.londonpower.com/2-channel-guitar-preamp
If I understand correctly, the problem has two parts:
to eliminate the underscores, as they baffle the .htaccess engine;
to then perform a 301 redirect on the URL.
So far, I have been able to get the first underscore to change to a hyphen, with this Rewrite Rule:
RewriteRule ^([^_]*)_(.*)$ /$1-$2 [R=301,L]
...but no luck with the second underscore (the one that is part of the query string after the "?"). I am stuck there.
I would avoid using rewriting for this. Does the file catalog/product_info.php exist in the new store? If not, create it and add a simple redirection using a map of old IDs to new URLs. If so, do the same thing in a different file, like old-redirector.php then rewrite requests to it.

htaccess redirection matching urls to grandparent directory

A search engine keeps hitting my site regularly for non-existing pages that it randomly generates. I want to put an end to this silliness using a 301 rule in my htaccess file.
The most usual address is
site.tld/blog/welcome-back/x/xx/
where x is a random number but sometimes I get hits on
site.tld/blog/welcome-back/xx/xx/
or text instead of numbers.
I want to redirect all these hits to the real existing page
/blog/welcome-back/
Thanks in advance for any help!
This should do what you need:
RewriteEngine On
RewriteRule ^blog/welcome-back/.+$ http://site.tld/blog/welcome-back/ [R=301,L]

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 urls with specific string for .htaccess

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]

Redirect htaccess Based On Partial Folder Name

I have a set of dated folders I want to move into one container folder. That is
20011104
20011008
will now be in
archive/20011104
archive/20011104
is there any way to htaccess redirect these in a few lines, rather than one redirect for each. There are hundreds. Is it possible to do a wildcard like 200* such that all such requests get redirected into the archive folder?
Yes, since you know it will start with 200, and will be 8 digits long, you should be able to match with this:
RewriteRule (200\d{5})$ /archive/$1 [L,QSA]
Edit: I'm assuming you're using mod_rewrite

Resources