.htaccess rewrite for orphaned URLs containing underscores and arguments - .htaccess

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.

Related

Changing Domain: 301 Redirect for multi language E-Commerce site

I am moving my WP ecommerce site to a new domain and I need to code a more advanced htaccess 301 redirect to pass on the SEO love. (I say htaccess but maybe there is a server side way that is better)
I have made sure, as much as possible, to keep the URL structure the same so for the products/posts, categories, tags and most pages everything after the olddomain.com/XXXXX is the same.
However, I don't want to do a blanket redirect for everything because there are some parts of the site that will not match so I thought it better to break into into chunks/functions.
(Maybe this is a bad strategy and I should just do do one blanket redirect and the trouble shoot page not found as it all goes live?)
redirect function for products
redirect function for categories
redirect function for tags
individual redirects for the rest
There are also three languages with sub folder /ca/ and /es/ - example.com/es/products - assuming I can just copy the function for each language and appending the subfolder.
Examples:
oldomain.com/product/any-product-ABC
redirect to
newdomain.com/product/any-product-ABC
(Domain change) (folder same) (product added from previous)
Then same redirect for languages
oldomain.com/es/product/any-product-ABC
redirect to
newomain.com/es/product/any-product-ABC
How do I write the above redirects?
I say htaccess but maybe there is a server side way that is better
If you have access to the Apache server config then you can indeed simplify these redirects, which will also be more efficient since it will reduce the load (if any) from the main site.
Create a separate <VirtualHost> container for the old domain and then you can use simpler mod_alias directives.
For example, for the two examples you gave. eg. /product/<product-name> or /<lang-code>/product/<product-name> to the same URL at the new domain then you can use following single rule:
RedirectMatch 301 ^(/[a-z]{2})?/product/[\w-]+$ https://newdomain.com/$0
This matches any optional 2-character language code. If you only have three languages then you could be more specific and use regex alternation instead, eg. (ca|es|id) in place of [a-z]{2}. The <product-name> is assumed to consist of the following characters only: a-z, A-Z, 0-9, _ (underscore) and - (hyphen).
However, if you are restricted to .htaccess and both domains resolve to the same place then you will need to use mod_rewrite and check the requested hostname. For example, the following would need to go near the top of the root .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain\.com [NC]
RewriteRule ^((ca|es|id)/)?product/[\w-]+$ https://newdomain.com/$0 [R=301,L]
Note the regex is slightly different to the mod_alias RedirectMatch directive used above since the RewriteRule pattern (first argument) matches against the URL-path less the slash prefix.
Do not repeat the RewriteEngine directive if it already occurs elsewhere in the config file. The order of the directives is important. The above redirect must go before any existing internal rewrites and ideally before any canonical redirects (to minimise the number of redirects).
You should first test with 302 (temporary) redirects to avoid potential caching issues.
so I thought it better to break into into chunks/functions.
That's fair enough. Although you need to make sure that any URLs that you don't redirect from the old domain return a 404 or 410.
And it may be possible to combine "products", "categories" and "tags" into a single rule, depending on exactly the format of these URLs.

Rewriting rules in htaccess to add hyphens between names

I am not sure if this is even possible, but am trying to rewrite some urls in my htaccess file using one line of code rather than to add a separate rewrite for each of my hundreds of urls.
The existing urls are like the one below where the car make and model is in one string.
www.exampledomain.co.uk/cars/astonmartindb9/
I want to separate the make and model by adding a hyphen as below
www.exampledomain.co.uk/cars/astonmartin-db9/
This will do it:
RewriteEngine on
RewriteRule ^cars/(astonmartin|ford|renault)([^/-]+)/$ /cars/$1-$2/ [R=301,L]
You need to list all the different makes, separated by pipe characters, as shown. Should be the simplest solution.

Complex htaccess URL and variable rewriting

I run a mmo game fan site http://www.ddmsrealm.com. In this site I have run a databse with quest and item information for years. It was originally built with MSSQL/ASP and I have recently converted it to MYSQL/PHP. In this conversion I optimized the database. In doing so I changed a bit of the structure and variables. Now, the new pages are up and running but I am having a terrible time trying to write rules in my htaccess file to accommodate the hundreds of quests and thousands of items to reroute to the new database pages with the new database variables. Any and all help would be appreciated as I struggle to learn apache/htaccess.
I have two “details” pages that need to be rerouted along with having the variable names changed. The actual ID's are still the same so it should work.
Original Quest Page was an asp page:
~http://www.ddmsrealm.com/ddo/quests/QDetail.aspx
Permanent reroute to new Quest Details Page(WordPress Template Page):
~http://www.ddmsrealm.com/index.php/dungeons-and-dragons-quest-and-magic-item-database/dungeons-and-dragons-online-quest-info
Now the parameters need to be rewritten like this:
Old: QuestID to New: ddoQuestID
Old: SeriesID to New: ddoSeriesID
So the idea is that this:
~http://www.ddmsrealm.com/ddo/quests/QDetail.aspx?QuestID=210&SeriesID=30
Is permanently redirected as this:
~http://www.ddmsrealm.com/index.php/dungeons-and-dragons-quest-and-magic-item-database/dungeons-and-dragons-online-quest-info?ddoQuestID=210&ddoSeriesID=30
If I can get the formula and steps for this I am sure I can apply it to the other pages. Being new to htaccess rewriting I am struggling to understand what exactly needs to happen and in what order. As you can imagine, this has jacked up my SEO bad having about 5000+ pages now going to a 404. Plus hundreds of websites have linked that old URL and now those links are trashed until I can resolve this.
Thank you so much in advance for the help figuring this out!
The URI rewrite itself is pretty straight forward using a RewriteRule but changing the query string you'll need to use a RewriteCond and match against the %{QUERY_STRING} variable and backreference the matches using the % symbol. See the docs for more info: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
Try something like this:
RewriteEngine On
# Match the query string
RewriteCond %{QUERY_STRING} QuestID=([0-9]+)&SeriesID=([0-9]+)
# Rewrite the URI and append the new query string
RewriteRule ^ddo/quests/QDetail.aspx$ /index.php/dungeons-and-dragons-quest-and-magic-item-database/dungeons-and-dragons-online-quest-info?ddoQuestID=%1&ddoSeriesID=%2 [R=301,L]

Compress .htaccess file

I've got a htaccess file which contains over 3,000 lines mainly thanks to 301 redirects I have setup from my old ecommerce site. The file is 323kb in size and I'm worried it's going to be a burden for load times and therefore conversions.
Is there anything available that can compress (minify?) the file into a smaller size or someone offer a better idea to handle the 301 redirects?
If the redirects are simple redirects i.e. url1 to url2, no regex etc, AND you have access to httpd.conf, then you could use a RewriteMap for all the redirects and possibly have just 1 rule in your .htaccess to handle these.
From the RewriteMap documentation
The looked-up keys are cached by httpd until the mtime (modified time) of the mapfile changes, or the httpd server is restarted. This ensures better performance on maps that are called by many requests.
Can you specify some regular expressions to group / match all of these redirects? This then offers two options for doing this:
The first is to use a (hopefully smaller) set of RewriteRule statements using the [R=301] flag.
The second is to move this redirection into a redirector script where you use, say, PHP logic to decode the legacy ecommerce URI into its current format then issue a response with and 301/302 status and Location: pointing to the current URI. This would also need you to do a catch-all rewrite of the legacy ecommerce URIs to this redirector script, e.g.
RewriteRule ^(product/.*) rewriter.php?uri=$1 [QSA,L]
Without some examples, I can't give a more specific reply. Sorry.
I've had some of these cases before, most of the times you can replace the redirect statments with RewriteRules. For example, if your URL's went from:
http://shop.example.com/shop/category/product-id.html
To this:
http://shop.example.com/category/product-id.html
You can fetch it with a rewrite like this:
RewriteRule ^/shop/([a-z]+)/([0-9]+)\.html$ /$1/$2.html [L, R=301]
This will still result in a 301 redirect, so crawlers will still know it's a permanent move.

using mod_rewrite to create SEO friendly URLS

I've been searching google for this but can't find the solution to my exact needs. Basically I've already got my URL's named how I like them i.e. "http://mysite.com/blog/page1.php"
What I'm trying to achieve (if it's possible!) is to use rewrite to alter the existing URLS to: "http://mysite.com/blog/page1"
The problem I've come across is I've found examples that will do this if the user enters "http://mysite.com/blog/page1" into the broweser which is great, however I need it to work for the existing links in google as not to loose traffic, so incoming URLS "http://mysite.com/blog/page1.php" are directed to "http://mysite.com/blog/page1".
The 1st example (Canonical URLs) at the following is pretty much what you want:
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html#url
This should do the trick, rewriting requests without .php to have it, invisible to the user.
RewriteEngine On
RewriteRule ^/blog/([^.]+)$ /blog/$1.php
You will need to write a rewrite rule for mapping your old url's to your new url as a permanent redirect. This will let the search engine know that the new, seo friendly url's are the ones to be used.
RewriteRule blog/page1.php blog/page1 [R=301,L]

Resources