mapping RewriteRule to RewriteMap - apache

we're doing a migration and I need to redirect several hundred thousand old urls to new locations. I can get a proof of concept working with mod_rewrite but I know this will not scale. I've been reading about RewriteMap and I think it will do what I need but I can't get it to work.
the search strings can appear anywhere in the url making things more complicated.
the rewrite rules that work look like this
RewriteRule "876f7103-73fd-470b-a5bc-584bcd5135da" "https://XXXXX/V/Redirect?oldDirectLink=https://YYYY/Watch/y4L9TeKs"
RewriteRule "y4L9TeKs" "https://XXXXX/V/Redirect?oldDirectLink=https://YYYY/Watch/y4L9TeKs"
I can't figure out how to convert this to rewritemap... any help would be appreciated...

Related

Mass re-write rule that keeps the existing URL structure and only rewrites the end of the URL?

I'm currently working on a plumbing website which has both multi lingual and multi regional content, below are some examples of the current URL structure...
http://www.domain.com/product/de/?region=uk
http://www.domain.com/product/plumbing-and-heating/de/?region=uk
http://www.domain.com/product/plumbing-and-heating/de/?region=uk
For SEO purposes i'm looking to rewrite the end of these URLs to be...
http://www.domain.com/product/de-uk
http://www.domain.com/product/plumbing-and-heating/de-uk
http://www.domain.com/product/plumbing-and-heating/de-uk
I would also settle for...
http://www.domain.com/product/de/uk
http://www.domain.com/product/plumbing-and-heating/de/uk
http://www.domain.com/product/plumbing-and-heating/de/uk
I know I could do something like...
RewriteCond %{QUERY_STRING} (^|&)region=uk($|&)
RewriteRule ^product/de/$ /product/de-uk/? [L,R=301]
which would rewrite...
http://www.domain.com/product/de/?region=uk to be http://www.domain.com/product/de-uk/
However as there are obviously a lot of different languages, regions and subdirectories to do these one by one would be a massive task as their are many different combinations!
Therefore i'm looking for an easier more manageable solution, any help appreciated?
Something like this might help:
RewriteRule ^([^/]+)/([^/]+)/([^/-]+)-([^/-]+)$ /$1/$2/$3/?region=$4 [NC,L]
RewriteRule ^([^/]+)/([^/-]+)-([^/-]+)$ /$1/$2/?region=$3 [NC,L]

mod_rewrite for favicon control?

I'm making several subdomains as what will basically be portals to the same site on Namecheap. Redirecting subdomains is actually really easy (especially since the plumbing is hidden from me),but I want the favicons to be different. This is crucial because the site is crawled by robots that probably don't care about Javascript or the like.
How would I get a request for http://newsubdomain.example.com/favicon.ico to go to http://oldsubdomain.example.com/differentfavicon.ico instead?
Since I'm a huge n00b in mod_rewrite and most of .htaccess in general, I don't know if it's significant that I'm ultimately storing the files in a structure similar to
http://example.com/oldsubdomain/differentfavicon.ico ...
I could probably use PHP if worse came to worst, but I'm trying to avoid adding yet another language to the list of things my little project requires.
How would I get a request for http://newsubdomain.example.com/favicon.ico to go to http://oldsubdomain.example.com/differentfavicon.ico
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} =newsubdomain.example.com
RewriteRule ^(favicon\.ico)$ http://oldsubdomain.example.com/different$1 [L,NC,R=301]

mod_rewrite on parent and subfolder

I ran into some strange behaviour using mod_rewrite under Apache. Here's how the current RewriteRules look:
RewriteRule ^(.*)/collections/(.*)/(.*)/?$ bootstrap.php?controller=category&user=$1&collection=$2&category=$3 [L]
RewriteRule ^(.*)/collections/(.*)/?$ bootstrap.php?controller=collection&user=$1&collection=$2 [L]
My expectations of the above rules is that accessing a URL such as domain.com/BenM/collections/0/1/ should take me to the category controller, while domain.com/BenM/collections/0/ should take me to the collection controller.
At the moment, both URL structures rewrite to bootstrap.php?controller=category....
My understanding was that if the [L] flag is specified, Apache looks no further and performs the rewrite.
Could anyone point in the right direction here, as I just cannot beat this... What should the rewrites look like to achieve the functionality I explained above?
You're dealing with greediness-related issues. Also, you probably want a length of at least one, and you almost certainly don't want slashes in your params.
Potential solution:
^([^/]+)/collections/([^/]+)/([^/]+)/?$
^([^/]+)/collections/([^/]+)/?$

mod_rewrite, trailing slashes - one line instead of two?

I have been playing with mod_rewrite using .htaccess to translate some directories - for purposes of both improved SEO and also to produce friendlier/more memorable URLs.
The only problem I can't crack at the moment is with trailing slashes. The behavoir I want is that you should be able to access the link with or without a trailing slash, just to cut down on missed traffic.
My real url is as:
http://www.mydomain.com/shipyard/index.php
I would like people to be able to access it via:
http://www.mydomain.com/shipyard/
http://www.mydomain.com/shipyard
http://www.mydomain.com/ships/
http://www.mydomain.com/ships
Of course, the top two are covered because thats actually a real and accessible URL, but I plan to tell Google that the best way to get at the page is using /ships (without having to move directories, break existing links etc).
The best I came up with so far was:
RewriteRule ^ships/$ /shipyard/index.php [L]
RewriteRule ^ships$ /shipyard/index.php [L]
However I just KNOW that i'm using two lines where only one is needed, but whatever I tried, I couldn't get the one! I know i'm missing something incredibly basic and/or obvious, but I need a pointer... Thanks!
RewriteRule ^ships(/)?$ /shipyard/index.php [L]
This means that the slash may or may not be present.

Creating rewrite rules for multiple urls in the same folder

I have been asked by our client to convert a site we created into SEO friendly url format. I've managed to crack a small way into this, but have hit a problem with having the same urls in the same folder.
I am trying to rewrite the following urls,
/review/index.php?cid=intercasino
/review/submit.php?cid=intercasino
/review/index.php?cid=intercasino&page=2#reviews
I would like to get them to,
/review/intercasino
/submit-review/intercasino
/review/intercasino/2#reviews
I've almost got it working using the following rule,
RewriteRule (submit-review)/(.*)$ review/submit.php?cid=$2 [L]
RewriteRule (^review)/(.*) review/index.php?cid=$2
The problem, you may already see, is that /submit-review rewrites to /review, which in turn gets rewritten to index.php, thus my review submission page is lost in place of my index page. I figured that putting [L] would prevent the second rule being called, but it seems that it rewrites both urls in two seperate passes. I've also tried [QSE], and [S=1]
I would rather not have to move my files into different folders to get the rewriting to work, as that just seems too much like bad practise. If anyone could give me some pointers on how to differentiate between these similar urls that would be great!
Thanks
(Ref: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html)
What I would do, is make /submit-review/ post directly to itself (or a php file) then once submitted redirect from within the PHP file.
It can be hard to force htaccess to maintain post values whilst redirecting etc
My friend found a solution to this one.
RewriteRule review/submit.php - [L]
Will catch the first rewrite and then prevent the next one, worked a treat!